I have a number of files inside a folder that I am trying to run through a batch render command for command line rendering. The batch works, but I want to access the name of each file and set it as output name for the generated png file. Here is my batch command:
set process_dir=%CD%
C:
cd C:\solidangle\mtoadeploy\2016\bin
for /r %process_dir% %%f in (*.*) do kick -set defaultArnoldDriver#driver_png.RGBA.filename "C:\Arnold\batch\file_name.png" -l ../shaders -dw -v 4 -t 6 %%f
so for example in the path "C:\Arnold\batch\file_name.png" I want to replace the 'file_name' with the name of the file that is executed in order to produce that png file. Any help would be much appreciated!
I am not familiar with the render application you are using but I can suggest you the following way.
Replace file_name with %%~nf in your example.
To learn more about this feature and other read help with
FOR /?
Related
I need to convert some xls files into xlsx files. I can successfully convert one xls file into xlsx by running this command into cmd prompt (windows):
ssconvert inputFileName.xls outputFileName.xlsx
(ssconvert is a Gnumeric's command-line utility that can convert between different spreadsheet file formats)
I'd like to write a batch file that FOR EACH file in a specified directory runs the command I wrote above, using the current file name both for input and for output filename.
For example, if I have this set of files:
c:\directory\file1.xls
c:\directory\file2.xls
c:\directory\file3.xls
the output should be
c:\directory\file1.xlsx
c:\directory\file2.xlsx
c:\directory\file3.xlsx
so the batch pseudo code should be something like
directory = c:\directory\
for (fileName in directory)
ssconvert fileName.xls fileName.xlsx
Can anyone help me?
for /r %%v in (*.xls) do ssconvert "%%v" "%%vx"
a couple have people have asked me to explain this, so:
Part 1: for /r %%v in (*.xls)
This part returns an array of files in the current directory that have the xls extension. The %% may look a little curious. This is basically the special % character from command line as used in %PATH% or %TEMP%. To use it in a batch file we need to escape it like so: %%PATH%% or %%TEMP%%. In this case we are simply escaping the temporary variable v, which will hold our array of filenames.
We are using the /r switch to search for files recursively, so any matching files in child folders will also be located.
Part 2: do ssconvert "%%v" "%%vx"
This second part is what will get executed once per matching filename, so if the following files were present in the current folder:
c:\temp\mySheet.xls,
c:\temp\mySheet_yesterday.xls,
c:\temp\mySheet_20160902.xls
the following commands would be executed:
ssconvert "c:\temp\mySheet.xls" "c:\temp\mySheet.xlsx"
ssconvert "c:\temp\mySheet_yesterday.xls" "c:\temp\mySheet_yesterday.xlsx"
ssconvert "c:\temp\mySheet_20160902.xls" "c:\temp\mySheet_20160902.xlsx"
Actually this is pretty easy since Windows Vista. Microsoft added the command FORFILES
in your case
forfiles /p c:\directory /m *.xls /c "cmd /c ssconvert #file #fname.xlsx"
the only weird thing with this command is that forfiles automatically adds double quotes around #file and #fname. but it should work anyway
you can run something like this (paste the code bellow in a .bat, or if you want it to run interractively replace the %% by % :
for %%i in (c:\directory\*.xls) do ssconvert %%i %%i.xlsx
If you can run powershell it will be :
Get-ChildItem -Path c:\directory -filter *.xls | foreach {ssconvert $($_.FullName) $($_.baseName).xlsx }
I am doing similar thing to compile all the c files in a directory.
for iterating files in different directory try this.
set codedirectory=C:\Users\code
for /r %codedirectory% %%i in (*.c) do
( some GCC commands )
OS: Windows 10 Professional
I'd like to convert multiple PDF files to text files in main and sub-directories using GhostScript on Windows command line. Here's my command below:
gswin64c -sDEVICE=txtwrite -o test1.txt "test1.pdf"
This code converts "test1.pdf" to "test1.txt" using Ghostscript but I would like to perform the following:
Look for all pdf files in main directory and all sub-directories
Execute Ghostscript on all PDF files
Give the same file name to the output text file. (test1.pdf -> test1.txt)
I appreciate your time and consideration on this!
Build a batch or .cmd file like this:
#echo off
REM Replace these with your actual location
D:
cd "\Main Directory"
for /R %%F in (*.pdf) do call :DOPDF "%%F"
goto ENDIT
:DOPDF
for %%X in (%1) do set PDF_TXTFNM=%%~dpnX.txt
gswin64c -sDEVICE=txtwrite -o "%PDF_TXTFNM%" %1
goto :EOF
REM Clean up
:ENDIT
set PDF_TXTFNM=
EDIT TO ADD:
Side Note: If you do not wish to change your working directory to the main directory, be aware that for /R %%F in ("D:\Main Directory\*.pdf") ... will only work if there actually is a .pdf file in D:\Main Directory. The two main workarounds are to make it the current working directory (the solution chosen in my example) or to force a dummy .pdf file to exist in that directory and then choose to not process it in the subroutine using an IF statement. Holler if you need an example of that latter technique.
I am very new to writing batch files, but I am trying to automate some data updating I have to do. I have the pdftotext.exe in a folder with a pdf file that I get each month. I was hoping to use a wildcard in the batch file so that I don't have to change the code each month. So right now I run:
pdftotext -raw -nopgbrk CO_Aug16.pdf
This will run the conversion with the pdf and the executable in the same folder, but if I try to use a wildcard in the filename:
pdftotext -raw -nopgbrk CO*.pdf
It looks for a file named that way, so I tried to create a variable called fname and put the wildcard in that name, but when I run:
set fname = "C:\Users\...\CO*.pdf"
pdftotext -raw -nopgbrk "%fname%"
It acts like it is trying to open a blank file, I get:
I/O Error: Couldn't open file ''
Is it possible for me to be able to use the wildcard? Sorry if this is a basic question, I just started learning to write in a batch file for this project.
Handling of wildcards is done by the applications, not by the terminal.
The approach I use is to find the files using dir *.pdf and then for each file call the command.
for /F "tokens=*" %%f in ('dir /b *.pdf') do pdftotext -raw -nopgbrk "%%~f"
If you're entering this in a batch file use 2 percentage signs. If you're writing direct into cmd then just use 1.
Description:
For /F - run a command for each line of another commands output
tokens = * - Put all the line content into %f
dir /b *.pdf = the command we loop over. Finds all pdfs. Only outputs the minimum of data.
Your command - the %%~f is the filename. The ~ strips any quote marks
I'm not very familiar with windows batch scripts except very simple applications. I'm currently trying to figure out how to run a batch file in a local directory, scan the directory for each *.zip file and then create a corresponding .txt file (in the same local directory). This will run indefinitely at intervals of say... 5 seconds. Any clues to help? Would it be easier to write a Java program or something?
You can use the FOR command to iterate through files in a directory. The TYPE command creates an empty file.
#echo off
FOR %%G IN (*.zip) DO TYPE nul>"%%~nG.txt"
I got it:
#echo off
:loop
FOR %%G IN (*.zip) DO TYPE nul>"%%~nG.txt"
timeout /t 300
goto loop
I'm working on a CMD line batch file in a Win7 environment that will create directories based upon the filenames listed in a directory.
I am using this code, but the output created is partial and incomplete with only
setlocal enabledelayedexpansion
for /r %%i in (*.wav) do (
set filename1=%%i
set folder1=!filename1:~4,10!
mkdir !folder1!
)
pause
I have this script saved as a CMD file in text format in the source directory, on a local harddrive, though it is in a subdirectory.
The directory output is partial and broken, with garbled output and the numbers of directories created does not match the number of files, and the created directories seem to nest. I've researched this and can't seem to find a definitive answer.
It's not entirely clear what it is you are trying to accomplish. Are you trying to create a directory within the same directory containing the wav file, just without the .wav extension? If so, you're missing some quotation marks and you're stripping the wrong end of the filename off. If that's what you are trying to accomplish, it can actually be done with a single command, no batch script needed. Type this at the command prompt:
for /r %I in (*.wav) do mkdir "%~pnI"
Of course, if you still want it in a batch script, use %%I and %%~pnI with double percents instead of single. See the last couple of pages of help for for an explanation of how %%~pnI works.