Rename Files having EmDash using a Batch File - batch-file

I have a ton of files containing em dashes within the file name.
I am having trouble renaming them, as cmd apparently fails to recognize it, and converts emdash into a regular dash.
As a result I get the following error after I run the ren command:
ren "MyFile With – EmDash.txt" "MyFile.txt"
The system cannot find the file specified.
Things I have tried
Inserting chcp 65001 at start of batch code.
This command does not seem to work.
Saving the batch file as ANSI in Notepad
What this does is basically the following:
ren "MyFile With û EmDash.txt" "MyFile.txt"
The system cannot find the file specified.
As one can guess apparently saving ANSI format changes a character stored as em dash in notepad to û when batch file runs in a cmd window.
I have got dozens of files which need this renaming, and this would be monotonous to do so without a batch script.

Related

Create a batch file to run an .exe with one argument

I want to create a batch file that a user can run ... in the batch file I want to run an exe with one argument.
Here is what I have today:
#echo off
c:\
cd "C:\Program Files (x86)\App Location\App34\"
start HelperSetup.exe -arg
When I run that it opens up the cmd window and says the path cannot be found but i know for 100% it is the correct path.
I have tried to also pass in the string in a one line but no joy
"C:\Program Files(x86)\App Location\App34\HelperSetup.exe -arg"
I have tried to also pass in the string in a one line but no joy
When you want to also pass in the string in a one line you need to set the closing quote at the end of the path like this:
"C:\Program Files(x86)\App Location\App34\HelperSetup.exe" -arg
A much simpler approach for your batch script is to use the following command sequence
start /d "C:\Program Files (x86)\App Location\App34\" HelperSetup.exe -arg
This way, you don't need to change the drive and the cd command at all.

Create a batch file to run a command with arguments multiple times

I use the following command to export data from a source file to target file in CSV format.
C:\MyApp.EXE -export "C:\Test\Sample.dat" "C:\Test\results.CSV"
However I need to repeat the same command multiple times just by changing the source and target files. something like this
C:\MyApp.EXE -export "C:\Test\Sample01.dat" "C:\Test\results01.CSV"
C:\MyApp.EXE -export "C:\Test\Sample02.dat" "C:\Test\results02.CSV"
C:\MyApp.EXE -export "C:\Test\Sample03.dat" "C:\Test\results03.CSV"
I'm looking to create a batch file to do the job. I have tried the following in a batch file and ran, but it is opening multiple console windows all at the same time. I want all this to happen in just one Command window and run the commands one after the other.
cd "C:\Test\"
start MyApp.EXE -export "C:\Test\Sample.dat" "C:\Test\results.CSV"
start MyApp.EXE -export "C:\Test\Sample01.dat" "C:\Test\results01.CSV"
I want code to create a batch file which runs MyApp.exe multiple times with arguments.
I'm using PowerShell to generate the batch file, so I don't need variables in the .bat file.
This task could be done with following batch file:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
for %%I in ("C:\Test\Sample*.dat") do (
set "FileNameCSV=%%~nI"
set "FileNameCSV=!FileNameCSV:Sample=results!"
C:\MyApp.exe -export "%%I" "%%~dpI!FileNameCSV!.csv"
)
endlocal
Command FOR searches in specified directory C:\Test for all files matching the wildcard pattern Sample*.dat. For each file the fully qualified file name (drive + path + name + extension) is assigned to loop variable I.
The first command in body command block of FOR loop assigns just the file name to environment variable FileNameCSV. On this line a DAT file name with one or more exclamation marks would not be interpreted as most users expect. The exclamation mark(s) would be interpreted as beginning/end of a delayed expanded environment variable reference. However, this is no problem here according to file names in question.
The second SET command line uses a simple case-insensitive string substitution to replace all occurrences of sample by results in CSV file name.
The environment variable must be referenced with delayed expansion using !VariableName! syntax. Otherwise the Windows command line interpreter cmd.exe would expand (= replace) the reference of the environment variable on using %VariableName% on parsing entire command block starting with ( and ending with matching ) before FOR is executed at all.
The third command line executes your application with the input file name with full path and extension and the CSV file name also with full path of input file, but with modified file name and a different file extension.
But faster would be following batch code also working for files with ! in fully qualified file name.
#echo off
for %%I in ("C:\Test\Sample*.dat") do C:\MyApp.exe -export "%%I" "%%~dpI_%%~nI.csv"
ren "C:\Test\_Sample*.csv" "results*.csv"
The FOR loop executes your application with each *.dat as input file and with _*.csv as output file, i.e. _Sample.csv, _Sample01.csv, ...
The CSV files are renamed after finishing processing all DAT files to results.csv, results1.csv, ...
Adding the additional underscore is necessary to rename all _Sample*.csv correct to results*.csv. The number of characters before wildcard character * must be the same in current and new file name.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
for /?
ren /?
set /?
setlocal /?
But I do not really understand why all this is done with a batch file executed by Windows command line interpreter cmd.exe if this batch file is really created with a PowerShell script executed by script interpreter powershell.exe. All this can be done also from within the PowerShell script by using the appropriate PowerShell script functions.

Batch File to zip files - apply to sub directories

I'm looking to get a batch file to apply to all sub directories.
I have a number of folders. Each folder will contain file pairs
xxx1.mp3 and xxx1.cdg,
xxx2.mp3 and xxx2.cdg,
etc.
I have a 7zip batch file that will look for file pairs and create a single zip file xxx1.zip, xxx2.zip and delete the (now) redundant cdg/mp3 files.
However, this will only work if the bat file is run in each individual folder. What I'm really looking for is a switch to add to the bat file that if I run in the root directory, will run through all sub directories also.
The code I currently run is:
FOR %F IN (*.cdg) DO "C:\Program Files\7-Zip\7Z.exe" a "%~nF.zip" "%~nF.cdg" "%~nF.mp3" -sdel
Any help?
FOR /R %%F IN (*.cdg) DO IF EXIST "%%~dpnF.mp3" pushd "%%~dpF"&ECHO("C:\Program Files\7-Zip\7Z.exe" a "%%~nF.zip" "%%~nF.cdg" "%%~nF.mp3" -sdel&POPD
Note that this is a batch line - to execute from the prompt, reduce each %% in metavariable to % (but it's a whole lot easier to put it into a batch by cut-and-paste;saves all those typos...)
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, delete the string ECHO( which appears before your command to actually execute the commands.
Using for /r will iterate through the filenames matching the supplied mask (it's possible to also add a starting directory - see for /?|more from the prompt.)
Since we know the selected full filename exists, first check that the paired file exists, then switch to the required directory, archive, and switch back.

BATCH> run .exe files which paths are contained in a text file

I'm working on a start script, and I have a text file output.txt, containing paths to programs, like:
C:/program1.exe
C:/abab/program2.exe
How do I now run the programs contained in the text file via a batch script?
for /f "usebackq delims=" %A in ("C:\output.txt") Do Start "" "%A"
Start starts programs without waiting for them to exit (so new window) and first set of quotes is the window title. UseBackq is needed to use quotes around output.txt. See For /? and my answer here for how to start programs Trouble with renaming folders and sub folders using Batch. In a batch use %%A and %A when typing interactively (I don't use batch files, I keep stuff in one text file and paste parts into a command prompt window,which is interactive).
If they were to be run sequentially then
Rename the file to batch and run it. Open in notepad and search for / and replace with \ as that is the windows standard (although autocorrect will fix it without telling you but can sometimes cause problems).

Deleting a dll using a batch file

Am having a batch file used to update certain files.
However when i try to delete a file from a specified folder i am getting error "Invalid syntax, directory".
The command is below
del /Q %INSTDIR%\xyz.dll
The %INSTDIR% is taken from registry and is correctly processing any copy commands. But am getting error for del command. Please help
Try this:
del /Q "%INSTDIR%\xyz.dll"
Most likely %INSTDIR% has spaces in it sometimes and thus the failure. You must place double quotes around long file/folder names.

Resources