I have an executable file, call it exec1.exe
I have a bunch of files with a .txt extension and I want to run exec1.exe on it and redirect output to a text file with the original file name somewhere in the output file. I'm running the command
for %i in (mydir\\*.txt) do exec1 %i > "%i2.txt"
But this tries to run on the first text file text1.txt,
exec1 exec1text1.txt > exec1text1.txt2.txt
But I want
exec1 text1.txt > text1.txt2.txt
Any idea what's going wrong?
You could probably get away with this, (double up the % if running from a batch file):
for %i in ("mydir\*.txt") do #start "" exec1.exe -y 754 "%i">"%~ni2.txt"
Note
I have used %~ni2 instead of %i2 to write to the current directory. This is because your command would be outputting to .txt whilst reading *.txt. An alternative would be to use a different known path, e.g. >"known\%i2.txt
Related
I am using the PDFtk to remove last 2 pages of a bunch of PDF from a specific folder.
For removing it individually on a file, this code works perfectly fine as the last two pages are removed from original.pdf and a newly created reduced.pdf copy is created without the last two pages
#echo off
cd "C:\Program Files (x86)\PDFtk\bin"
start pdftk.exe C:\Desktop\long\original.pdf cat 1-r3 output C:\Desktop\short\reduced.pdf
pause
Fyi, the pdf files all have various alphanumeric filenames and a - as separator between filename words e.g. the-march-event-2022.pdf
What I need now is how to automate is so the script would go through each pdf file on the long folder and create a new copy with identical filename through the command into the short folder
The task can be done with a batch file with only following single command line:
#for %%I in ("C:\Desktop\long\*.pdf") do #"C:\Program Files (x86)\PDFtk\bin\pdftk.exe" "%%I" cat 1-r3 output "C:\Desktop\short\%%~nxI" && echo Successfully processed "%%~nxI" || echo ERROR: Failed to process "%%~nxI"
This command line uses the Windows command FOR to process all PDF files in the specified folder. For each PDF file is executed pdftk with the fully qualified file name of the current PDF file as input file name and the file name + extension with a different directory path as output file name. Run for /? in a command prompt window for help on this command.
There is output the success message on pdftk.exe exits with value 0. Otherwise the error message isĀ output on pdftk.exe exits with value not equal 0.
The two # are for suppressing the output of the FOR command line and of each executed pdftk command line on processing the PDF files in the specified folder.
Please see single line with multiple commands using Windows batch file for an explanation of the conditional operators && and ||.
I would like to create a simple script containing :
"C:\Users\Professional\NVEncC_5.24_x64\NVEncC64.exe" --codec h265 --preset quality --profile main10 --tier high -i "Project.mkv" -o "Project-Q27.mkv"
that i can reach by Right Click => SendTo.
I Understand that i need to name the file XXX.bat and paste it in SendTo folder.
But i don't know how to get the filename to dynamically add it to the script instead of "Project.mkv".
Can you help me ?
Thanks !
K.
A .bat file with the following should work.
"C:\Users\Professional\NVEncC_5.24_x64\NVEncC64.exe" --codec h265 --preset quality --profile main10 --tier high -i "%~f1" -o "%~n1-Q27.mkv"
pause
I also added the pause so you'll be able to see what happens if it's quick but it can be removed.
%~f1 is the argument for the file you've right clicked and used "SendTo" on (expanded to the full path).
%~n1-Q27.mkv" is essentially the same as argument %~f1 but without the file extension and it adds -Q27.mkv to whatever the filename was.
So if you right click/SendTo/yourbat.bat on a file called Funny.mkv the command (NVEncC64.exe) will be run on that file and output to a file called Funny-Q27.mkv.
I suggest you take backups of your files before testing so that you do not overwrite any existing files by mistake.
To get the name of the PowerShell .ps1 script being run, you can use the following command from MyInvocation:
$MyInvocation.MyCommand
This will return the .ps1 file object.
To get only the name string you could run:
$MyInvocation.MyCommand.Name
While the answer that #notjustme provided works, I have a small addition to it: the output variable should include the drive and path of the filename that was passed to the batch file to have the output file be created in the same directory as the source file.
"C:\Users\Professional\NVEncC_5.24_x64\NVEncC64.exe" --codec h265 --preset quality --profile main10 --tier high -i "%~f1" -o "%~d1%~p1%~n1-Q27.mkv"
pause
The filename that is passed when you right-click --> SendTo --> your_batch_file.cmd is referenced as %1 or the first parameter. Because it is a filename, it can be parsed and expanded further into its parts.
And as mentioned by #notjustme:
%~f1 expands %1 (the filename passed) to its fully qualified path name
%~n1 expands %1 to its file name only
But there is also:
%~d1 expands %1 to its drive letter
%~p1 expands %1 to its full path
%~x1 expands %1 to its file extension only
If you do not add the %~d1%~p1 to the output file variable, by default, the transcoded file will not be created in the same directory as the source file; rather, it will be created in the directory where your batch file is located.
(Additionally, given rigaya's rather short development windows with NVEncC, you might want to install it to a more generally-named directory that does not include the versioning, e.g., "C:\Users\Professional\NVEncC\" instead of "C:\Users\Professional\NVEncC_5.24_x64\". That way, you do not have to update its path in your batch file with every new release.)
I have multiple log files that start as ABC_.log in a windows environment. I want to clean that file (like writing /dev/null to file in linux). I need to do it through command line.
What I tried:
cmd:$ break > ABC_*.log
and
cmd:$ type NUL > ABC_*.log
Error:
The filename, directory name, or volume label syntax is incorrect
this can't be done via wildcard (not possible to redirect to more than one file at a time). Use a for loop to process each file on it's own:
for %%a in (ABC_*.log) do (
break>"%%a"
)
or directly on command line:
for %a in (ABC_*.log) do break>"%a"
The easiest way to empty a file in UNIX/Linux:
rm <filename>
touch <filename>
I have to say before I describe my problem that I am new in performing command line prompts (Windows 10).
I have a directory with txt files (among other files). I need to perform a command line executable (txt2las) where the txt files are read and a .las file is written. This is what I have so far as a command prompt for /R %f in (.\*.txt) do txt2las -i %f -o %f.laz
However, this creates files like name.txt.laz. What I want is name.laz, therefore somehow take the substring from beginning till the last four characters. Can someone help me how to do that?
I tried the substring "function" but does not write anything.
You can extract the just the name of the file (without the extension) by using the ~n modifier:
for /R %f in (.\*.txt) do txt2las -i %f -o "%~dpnf.laz"
You can read more about the modifiers in the FOR /? help docs.
Edit per comments:
To ensure it is placed in the same directory as the source, add the ~dp modifiers in addition to ~n. This will extract the drive and path in addition to the file name. Also, you will likely want to wrap this entire output in quotes in the event the path has spaces in it.
... do txt2las -i %f -o %~nf.laz
The documentation can be read by executing for /? from the prompt.
I have a file input.txt which contain multiple commands. I need to use that file as input of my batch file. How can i do this.
cmd < input.txt