0

Long time listener, first time caller.

I have variables stored in a .csv file that I use to generate .cmd files and also to setup scheduled tasks to run them.

Ex. of .cmd file:

C:\Python27\ArcGIS10.5\python.exe "D:\ScriptRepo\Script.py"
C:\Python27\ArcGIS10.5\python.exe "D:\ScriptRepo\Script2.py"
C:\Python27\ArcGIS10.5\python.exe "D:\ScriptRepo\Script3.py"

I require these tasks to run one after another.

I am storing the contents of the .cmd file in a cell in a .csv file with returns after each line. So the .csv cell looks the same as the intended contents of the .cmd file.

The problem is that the contents of the .cmd file are written in one long line:

C:\Python27\ArcGIS10.5\python.exe "D:\ScriptRepo\Script.py" C:\Python27\ArcGIS10.5\python.exe "D:\ScriptRepo\Script2.py"C:\Python27\ArcGIS10.5\python.exe "D:\ScriptRepo\Script3.py"

I have tried to insert `r`n after each line, but that just writes those characters inline. Ex. Line `r`n Line `r`n etc....

Question:

1) In a .cmd file can you declare the .exe of the program to run and then a list of scripts to run with that program? If so will they wait for the 1st script to complete before moving to the next one?

2) How can I write a new line into the .cmd file?

Current Powershell to write to the .cmd file:

$files = Import-Csv D:\scheduling.csv

FOREACH ($file in $files) 
{
    $full_cmd_path = $file.cmd_filepath += $file.cmd_filename
    New-Item $full_cmd_path -type file -force -value 
    $file.cmd_filename_content
}

screen cap of .csv file

Thanks for the help or thanks for blasting me for not finding the info on my own, or screwing up the formatting, or just because I'm bad at scripting.

Thanks for everything.

4
  • Could you post the csv file (or at least a sample with a few rows)? Commented Jan 31, 2018 at 19:28
  • Sorry, can't use any cloud storage services here in the dark ages. Posted screen cap though. Commented Jan 31, 2018 at 19:43
  • What character is splitting those lines? That looks like a multi line field. Commented Jan 31, 2018 at 19:51
  • I am just hitting return between lines in the csv cell. Commented Jan 31, 2018 at 21:07

1 Answer 1

1

The problem could be that $file.cmd_filename_content contains a multi-line string with unix-style line endings.

You could split the string into separate strings and then pipe them to Out-File instead:

foreach($file in $files) 
{
    $full_cmd_path = Join-Path $file.cmd_filepath $file.cmd_filename
    $content = $file.cmd_filename_content -split '\r?\n' |Out-File -Path $full_cmd_path
}

Out-File will add a windows-style line ending (CRLF/`r`n) after each line

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

2 Comments

Yeah. This is what I thought was happening as well. Looking at the characters [int[]][char[]]$file.cmd_filename_content would be another way to verify
Good point @Matt, I like to use 'string'.ToCharArray()|%{'{0,2} - {1,6}' -f $_,+$_} - gives context and makes weird unicode characters stand out clearly

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.