12

I'm attempting to copy/paste some long, multiple line command line commands into a Windows 10 Command Prompt.

Linux uses the "\" character for this, so for example in Linux you could copy/paste the following from a website or text file into a terminal Window (I'm supposing using a graphical desktop environment here):

python retrain.py \
    --bottleneck_dir="/tf_files" \
    --how_many_training_steps=500 \
    --model_dir="/tf_files" \
    --output_graph="/tf_files/retrained_graph.pb" \
    --output_labels="/tf_files/retrained_labels.txt" \
    --image_dir="/tf_files/flower_photos"

Then press enter and the command will run. This looks much more clear on a website or in a text file you've saved.

Problem is, I can't seem to work out a Windows equivalent for this. I realize the ^ character is equivalent to the Linux \ character in this context, but it does not seem to work for copy/paste. For example, if make a text file "dummy1.txt", then copy/paste in the following:

copy dummy1.txt ^ 
    dummy2.txt

(note there is a space on both sides of the ^ character on the 1st line)

I get this:

enter image description here

For this two line example this does not matter, but for more elaborate commands such as the python script above, putting everything on one line in any documentation that needs to be copies/pasted out of sacrifices readability severely. Is there a way to get this to work on Windows? I'm using Windows 10 currently if that matters.

4
  • You might want to look at this StackOverflow question on how to paste and remove newlines in Windows 10 cmd.exe: superuser.com/questions/566353/… Commented Jan 9, 2018 at 17:33
  • 1
    Working with the CMD shell interactively is difficult with complex, multi-line commands. The caret escapes the character that follows it, so you're escaping the space instead of the carriage return. Commented Jan 9, 2018 at 18:03
  • 3
    Alas, even the nice new Windows Terminal and it's rewritten command prompt doesn't address this. Even with correctly placed carets, cmd still interprets each line as a separate command when pasted :( Commented Dec 9, 2019 at 18:21
  • I think a better solution is to not use cmd.exe but rather use PowerShell instead. I think it is highly doubtful that this behavior will be changed in cmd.exe (the current behavior may be as it is due to a compatibility constraint.) Commented May 19, 2021 at 14:42

2 Answers 2

1

I think you need to powershellafy it like this:

$retrain =  @{
    bottleneck_dir = “/tf_files”
    how_many_training_steps = “500"
    model_dir= "/tf_files"
    output_graph = "/tf_files/retrained_graph.pb"
    output_labels = "/tf_files/retrained_labels.txt"
    image_dir = "/tf_files/flower_photos"
}
#imaginary equivalent script  
retrain.ps1 $retrain

If you are simply looking to break lines into multiple lines the escape character is a back tick `

get-longcommand -name "long command" `
-date "today" `
-other-info "other stuff"

Lastly, please understand a 'Foreach' loop. There are multiple kinds, here is an easy one

$copy_list = @(get-childitem "C:\Path\to\files")
$new_dest = "C:\New\File\Destination"

foreach ($file in $copy_list) { 

#This is for troubleshooting, outputs which file it is running
  $file
  copy-item -Path $file -Destination $new_dest
  }

There may be something like $file.fullname or something like that to transfer the path. If you use Powershell ISE, and use the debug routine, pause right after '$file' in the loop, and you can see the details.

Enjoy!

Sign up to request clarification or add additional context in comments.

2 Comments

The back tick ( ` ) is for multi-line commands in PowerShell. For the traditional windows terminal cmd.exe the character for that purpose is the caret ( ^ )
@CristobalSarome is dropping some pro tips yo.
1

Why try pasting a command with spaces after the caret? The example linux code given by the OP did not include spaces after the line breaking "\" backslashes. The comment by @Eryk explained that the caret needs to escape the line break.

Here are ready to paste snippets for the win 10 command prompt; the 1st without trailing spaces after the carets, and the 2nd with one space after its first caret.

dir C:\Windows ^
    /A-D ^
    /OS

dir C:\Windows ^ 
    /A-D ^
    /OS

Expect the 1st snippet, broken into three lines, to execute as a single command, and the 2nd snippet, with its trailing space, to unsuccessfully execute as two commands.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.