Ryan _
Ryan _

Reputation: 1

How to write to new line of .cmd file with Powershell from .CSV cell

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.

Upvotes: 0

Views: 171

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 175085

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

Upvotes: 1

Related Questions