Win 32, script, process all files in directory - windows

I need to process all txt file in some folder using a console application. The command looks similar to this:
convert input_file.txt -par1 -par2 -par3
How to write a batch processing all files in the folder, passing file name as parameter to console apllication? Another idea, what about a Python solution?

Use for
for %f in (*.txt) do #convert "%f" -par1 -par2 -par3
Note that I use # to suppress echoing of the command line at each iteration of the for loop. This is optional.
The above syntax is appropriate for use at the interactive console. When executing in a batch file, the %f must be replaced with %%f.

Related

Problems Executing Powershell from .cmd file

I am attempting to run a build task from a .cmd file where Powershell extracts a zip file, which helps bypass a problem with Visual Studio's limit on the number of directory characters. However, I am having problems getting the Powershell command to execute correctly. I've tried a number of variations with the quotations, and I either get a termination error, or the Powershell command outputs as a string with the zip file not extracted. Below is an example of my current .cmd file:
//%1% is a passed in command line argument for the absolute path, e.g. C:\path\to\dir
set Source=%1%directory.zip
set Destination=%1%directory
powershell -Command $pscmd = '{Add-Type -assembly "system.io.compression.filesystem";[io.compression.zipfile]::ExtractToDirectory("%Source%", "%Destination%");}'; Write-Host $pscmd;
I'm very open to a number of variations that can get this to work, provided that this task runs on the command line, uses Powershell, and can be executed from a .cmd file, which is triggered by our app's build process. I'll be happy to provide additional information if needed. Thanks!
This was a strange one. Your code above has some sort of hidden character in it. I took the code and opened it in notepad, saved as ANSI, and when you type it to command line or open it again in a new instance of notepad you can see the error.
Neither add-type nor ExtractToDirectory give output, so I removed your pscmd var.
I would open your existing script, save as ansi as a new file name, delete the original, rename the new one back to the original name.
Here is what I came up with to troubleshoot your script, and it works on my machine.
I named my script L:\util\unzip.cmd
setlocal
//%1% is a passed in command line argument for the absolute path, e.g. C:\path\to\dir
set _Source='%1\directory.zip'
set _Destination='%1\directory'
echo _Source=%_Source%
echo _Destination=%_Destination%
set _c1=Add-Type -assembly system.io.compression.filesystem;
set _c2=[io.compression.zipfile]::ExtractToDirectory(%_Source%, %_Destination%)
echo _c1=%_c1%
echo _c2=%_c2%
set _Command=^& {%_c1% %_c2%};
: to echo, use double quotes or cmd thinks text after & is another command
echo _Command="%_Command%"
pause
powershell -Command "%_Command%"
endlocal
I ran it like this, and it worked: unzip.cmd L:\util
I'll bet this this info, you are good to go.

Make System information Batch file

How to create a batch file about system info and ip address and domain server etc. _ which will generate a output as txt file
You can put whatever Windows commands you want in a batch file and then redirect the output to a text file using >. E.g., you could create a batch file called test.bat and put the following commands in it:
#echo off
systeminfo
ipconfig /all
Then you could run test.bat from a command prompt as follows:
C:>test >outfile.txt
The output of the batch file, which would consist of the output from the commands you placed in it, would be redirected to a file named outfile.txt by the redirect operator, >. Note: it can sometimes take awhile for the output of the systeminfo command to complete, so, depending on your system, you may need to give it a minute to complete.
Alternatively, you could add the >outfile.txt at the end of commands within the batch file itself, though for every instance after the first one you need to use double greater than signs, i.e., >>, to append the output rather than wiping out what was in outfile.txt previously and creating a new version of that text file. E.g., you could have the following in test.bat:
#echo off
systeminfo >outfile.txt
ipconfig /all >>outfile.txt
You would then just use the following at a command prompt:
C:>test

Windows batch file, save contents of file dowloaded by wget to a variable

Currently, I have a batch file that uses wget to read a file from the server. Is there any way for wget to save the contents of that file to a variable and then for the batch file to take a certain action based on the value of the variable?
The peseduo-code would probably look something like this. I am very new to batch files and am still learning the semantics:
SAVE RESULT OF wget http://www.theserver.com/instruction TO VARIABLE: the_variable
IF %the_variable% == 'restart' <DO SOME ACTION HERE>
I will base this answer on the assumption that your downloaded file contains text strings.
If this is the case then it is possibile to use the FOR command in this way:
for /F %I IN (instruction.txt) DO if %I==restart #echo RESTART FOUND
This command opens the file "instruction.txt" and parse it assigning each word to the variable %I
Then for each value of variable %I executes the command specified after the keyword DO.
In this case I have compared the variabile %I to the string "restart" and if the result is true the batch execs the command #echo RESTART FOUND
You could use the GOTO: function in your batch file. So that if the variable is equal to a certain value/string it jumps to a particular section in the batch file and carries out the code in that section.
Almost like using methids in Object Orientated Programming.
CHeck this link out :-
http://www.robvanderwoude.com/goto.php

Windows Batch Script: For ... in loop to capture program output not working

I have a batch script setup to automatically retrieve a file from a remote FTP server. Part of the requirement is the file will be named with a new datestamp each day, such as "File_90611.csv." I have a command line tool that generates the filename; which is then supposed to be set to a variable using the line below:
for /f "delims=" %a in ('C:\BIN\YesterdayDateStamp.exe') do #set DATESTAMP=%a
The problem is this. This line works fine when run from the command line directly. However, when I put this exact same line in a batch script and run it; I get this error:
\BIN\YesterdayDateStamp.exe') was unexpected at this time.
I REM'ed everything out in the script except the FOR ... IN commands to make sure there wasn't some sort of conflict; but even with this I still get an error.
Been Googling for an answer but have no leads. Any ideas? Any constructive input is greatly appreciated.
Thanks,
Frank
When for is used in a batch file, you need to double the percent signs in front of the variable name.
From for /?:
To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
from %I.

How to move output file in dos

I would like to create a batch file which will move the output file of a custom command "mdmg C:\source i5". I must execute this command from the C:\home directory where the mdmg.cmd resides.
This command converts the any file in the source dir and creates an output files in the C:\home folder.
However I want to move the output files to another folder autometically, lets say C:\test.
can it be done in a batch script?
Thanks in advance.
bla.bat
move c:\home\* c:\test
What is the problem ? DOS has a move command. Or you could simulate that with a copy and delete if move is unavailable for some reason.
You could save yourself the trouble of the batch using CMD redirects. Just paste the following behind the mdmg.cmd command.
> "C:\source i5\output.txt"
Basically the CMD interpreter will execute the command(s) in mdmg.cmd, and then redirect the output of the commands to print in output.txt. This way you don't have to call another batch. Another cool thing about doing it this way is that if the output file does not exist at the specified path, cmd.exe will make it for you.

Resources