Loop through each line of text file and run command against each - powershell

I am trying to adjust some code which is shown below and hitting walls.
The commandline appears as:
cmd.exe /U /C "C:\Program Files\StorageCraft\ShadowProtect\VerifyImages.cmd <PathOfDirectoryWhichContainsImageFiles> <PathToOutputLogFile>
The code basically runs an image verify command against all md5 files in a directory. The problem is that some directories have >200 md5 files and I only want to verify the files created in the last 24 hrs.
I have been able to create a list of the files created in the last 24hrs and output to a text file using a powershell command.
Is it possible to adjust the script below so that it reads the text file line by line and runs the VERIFY_SUB against each? I have tried using the FOR /F command with little luck to this point.
Thanks in advance.
REM *** START OF MAIN ROUTINE ***
SETLOCAL
PUSHD
CD /D %~dp0
REM Strip the outer quotes off of the directory parameter
SET PARAM_DIR=%1
SET PARAM_DIR=###%PARAM_DIR%###
SET PARAM_DIR=%PARAM_DIR:"###=%
SET PARAM_DIR=%PARAM_DIR:###"=%
SET PARAM_DIR=%PARAM_DIR:###=%
REM Strip the outer quotes off of the output log file parameter
SET PARAM_OUTPUT_FILE=%2
SET PARAM_OUTPUT_FILE=###%PARAM_OUTPUT_FILE%###
SET PARAM_OUTPUT_FILE=%PARAM_OUTPUT_FILE:"###=%
SET PARAM_OUTPUT_FILE=%PARAM_OUTPUT_FILE:###"=%
SET PARAM_OUTPUT_FILE=%PARAM_OUTPUT_FILE:###=%
FOR %%A IN ("%PARAM_DIR%\*.md5") DO (call :VERIFY_SUB "%%A" "%PARAM_OUTPUT_FILE%")
POPD
ENDLOCAL
GOTO :EOF
REM *** END OF MAIN ROUTINE ***
:VERIFY_SUB
#ECHO VERIFYING MD5 FILE %1
#ECHO VERIFYING MD5 FILE %1 >> %2
image.exe v %1 >> %2
#ECHO. >> %2
#ECHO. >> %2
#ECHO. >> %2
GOTO :EOF

Related

Automating the process of importing project into eclipse workspace using command line interface not working as expected

I have command line argument for importing existing project into eclipse work space. But as soon as I try to execute it using windows batch file eclipse opens and starts loading and closes immediately. Here is the code that I am trying to run.
ECHO on
PUSHD
SET ECLPSE=%cd%
SET WORKSPACE=%~dp0
POPD
SET PATH=%PATH%;%ECLPSE%\bin;
RD /s /q %ECLPSE%\configuration\org.eclipse.equinox.app > Nul
RD /s /q %ECLPSE%\configuration\org.eclipse.osgi > Nul
RD /s /q %ECLPSE%\configuration\org.eclipse.update > Nul
START /B %ECLPSE%\bin\tresos_gui.exe -Dmsg1085=false -data %WORKSPACE% %*
START /B %ECLPSE%\bin\tresos_gui.exe importProject -c C:\Jenkins\jobs
This is the syntax for importing a project into the workspace.
tresos_cmd.bat [<system_property>...] [-data <workspace>]
importProject [-c] <project path>...
Can someone please help me with this. I would really appreciate it. I have even combined last two statements in one line and tried executing it but it is of no use. My main aim is to automate the process of importing project into eclipse workspace so that software can be built using jenkins.
All folder paths containing %ECLPSE% or %WORKSPACE% should be enclosed in double quotes in case of %cd% and/or %~dp0 expand to a folder path with a space character or one of the characters &()[]{}^=;!'+,`~.
I did not really understand what is the goal of the batch file and what tresos_gui.exe is for.
However, here is an improved and commented batch file.
#echo off
setlocal EnableExtensions
rem Path of current directory hold in environment variable CD usually does
rem not end with a backslash (directory separator on Windows). But if the
rem current directory is the root directory of a drive, the directory path
rem ends with a backslash. Assign current directory path to environment
rem variable ECLPSE (strange name) always without a trailing backslash.
if not "%CD:~-1%" == "\" ( set "ECLPSE=%CD%" ) else ( set "ECLPSE=%CD:~0,-1%" )
rem Path of batch file always ends with a backslash, but should be assigned
rem to environment variable WORKSPACE always without a trailing backslash.
set "WORKSPACE=%~dp0"
set "WORKSPACE=%WORKSPACE:~0,-1%"
rem The environment variable PATH can end with a folder path or with a
rem semicolon after last folder path. The subdirectory BIN in current
rem directory should be appended with an additional semicolon only if
rem environment variable PATH does not already end with a semicolon.
if "%PATH:~-1%" == ";" ( set "PATH=%PATH%%ECLPSE%\bin" ) else ( set "PATH=%PATH%;%ECLPSE%\bin" )
rem Command RD does not print any message on success. It prints only an error
rem message to handle STDERR if the directory tree could not be removed.
rd /Q /S "%ECLPSE%\configuration\org.eclipse.equinox.app" 2>nul
rd /Q /S "%ECLPSE%\configuration\org.eclipse.osgi" 2>nul
rd /Q /S "%ECLPSE%\configuration\org.eclipse.update" 2>nul
rem Command START interprets first double quoted string as title for the
rem command process window. Therefore specify as first parameter just ""
rem which is an empty title string.
rem The start of tresos_gui.exe is done for some unknown reason in a separate
rem process in background. Hold execution of batch file until this separate
rem process terminated itself before running tresos_gui.exe a second time
rem to import the project and best wait again until this process terminated.
start "" /WAIT /B "%ECLPSE%\bin\tresos_gui.exe" -Dmsg1085=false -data "%WORKSPACE%" %*
start "" /WAIT /B "%ECLPSE%\bin\tresos_gui.exe" importProject -c C:\Jenkins\jobs
endlocal
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.
call /? ... explains %*
cmd /? ... explains with last paragraph on last help page when double quotes are required.
echo /?
endlocal /?
if /?
rd /?
rem /?
setlocal /?
start /?
And read also the Microsoft TechNet article Using command redirection operators for an explanation of 2>nul.

rename multiple files in order with command prompt

I have some files with different names.
Leviathan.txt,Dragon.txt and so on
I wanted to turn it into a digit begins
1.txt,2.txt,3.txt,4.txt and so on
how to perform like other language by using For and function that can pass amount files in folder?
my code so far i know is dir and ren. and i stuck now.
ren *.txt 1.txt
Next code snippet could work for you (save with .bat extension); note that rename command is echoed merely for debugging purposes:
#echo off
SETLOCAL enableextensions enabledelayedexpansion
set /A "ii=0"
pushd "working_directory_here"
for /F "delims=" %%G in ('dir /B /ON "*.txt" 2^>NUL') do (
set /A "ii+=1"
echo ren "%%~G" "!ii!%%~nxG"
)
popd
If you insist on an one-liner (launch in proper working directory):
cmd /E:ON /V:ON /K (#echo off^&set /A "ii=0" ^>NUL^&for /F "delims=" %G in ('dir /B /ON "*.txt" 2^^^>NUL') do (set /A "ii+=1" ^>nul^&echo ren "%~G" "!ii!%~nxG"))^&exit
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax
(%~G etc. special page) Command Line arguments (Parameters)
(EnableDelayedExpansion) Setlocal EnableDelayedExpansion
(^>, %% etc.) Syntax : Escape Characters, Delimiters and Quotes
Assuming none of your existing files are already named something like n.txt, where n is a number, then simply CD to your folder, and run the following command from the command line:
for "tokens=1* delims=:" %A in ('dir /b *.txt^|findstr /n "^"') do #ren "%B" "%A.txt"
Double up the percents if you use the command within a batch script.
EDIT
I forgot about my JREN.BAT utility - a regular expression renaming utility. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.
JREN has a built in ability to incorporate a number into each new file name, and as an added bonus, it can left pad the number with zeros so that a DIR command lists the files in numerical order. The default numeric width is 3 digits, so files would be like "001.txt", "002.txt', ... "010.txt", ... "100.txt", etc.
jren "^.*" "$n+'.txt'" /j /fm *.txt
The /NPAD option specifies the minimum numeric width, so NTAB 1 produces no padding, which is what the original question asked for.
jren "^.*" "$n+'.txt'" /j /fm *.txt /npad 1
Since JREN is a batch script itself, you must use CALL JREN if you put the command within another batch script.
Full documentation is available from the command prompt via jren /? | more. My console window is configured with a large buffer, so I can scroll back to see prior output, and I don't bother with piping the help to MORE.

Batch file: list all folders containing only a file entitled "Thumbs v0.1.db"

I need to scan through a drive and list all the folders containing only a single file, entitled "Thumbs v0.1.db". I have cobbled together the following code but it doesn't seem to work. Either the batch file exits prematurely, or it completes without listing any such subdirectories! I would be very thankful if someone could point out the problem.
#echo off
SET /P folder="Please enter root directory to seach in: "
SET writefile="C:\Users\MYNAME\Desktop\Thumbs.txt"
SET tmp="C:\Users\MYNAME\Desktop\rowcounttmp"
Echo Searching for directories, please wait...
echo Thumbs v0.1.db-only directories in %folder% > %writefile%
cd /D %folder%
for /d /r %1 %%A in (.) do (
dir /a /b "%%~fA" 2>nul | find /c /v "~StringWhichWillNotAppear~" > %tmp%
set var=<%tmp%
if [%var%] == 1 dir /a /b "%%~fA" 2>nul | findstr /i "Thumbs v0.1.db" >nul && echo %%~fA >> %writefile%
)
del %tmp%
Pause
Thanks
EDIT:
Thanks to #peter-wright, I now have this: Have I implemented his suggestions incorrectly? It seems to crash for certain filepaths (specifically the read-only drive I am trying to scan through) and there are still no results appearing.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
Color 0A
SET /P folder="Please enter root directory to seach in: "
SET writefile="C:\Users\MYNAME\Desktop\Thumbs.txt"
SET tempfile="C:\Users\MYNAME\Desktop\rowcounttmp"
Echo Searching for directories, please wait...
echo Thumbs v0.1.db only directories in %folder% > %writefile%
cd /D %folder%
for /d /r %1 %%A in (.) do (
dir /a /b "%%~fA" 2>nul | find /c /v "~StringWhichWillNotAppear~" > %tempfile%
set var=<%tempfile%
if !var! == 1 dir /a /b "%%~fA" 2>nul | findstr /i "Thumbs v0.1.db" >nul && echo %%~fA >> %writefile%
)
del %tempfile%
Pause
The variable var is being set within a BLOCK (parenthesised series of statements.)
Any %var% within a block is replaced by the value of that var WHEN THE BLOCK IS PARSED, not when it is EXECUTED.
To access the RUN-TIME value of var use !var! AFTER having invoked delayedexpansion with a SETLOCAL ENABLEDELAYEDEXPANSION statement (probably best implemented immediately after your #echo on.
Danger, Will Robinson : DO not use tmp as a variable name - tmp is a magic variable with special meaning to batch. Others may be listed by invoking
SET
from the prompt. Also avoid DATE, TIME, RANDOM, CD and a few others. And really not a good idea to use executable names or batch keywords either...
tip:
try using
FOR /F "delims=" %%i in ('dir /s /a /b /a-d "Thumbs v0.1.db" ') do (
and then %%~dpi will be assigned the names of the directories that CONTAIN the target file. If the count-of-files in these directories is not ==1 then it is not alone...

eol unix to windows in command line .bat batch

I am trying to convert text file eol to windows format from unix on windows xp machine using command line (batch file). how do I do that? what is the command for that? thanks.
This simple script is fast and works great except it converts every TAB character into 8 spaces. The number of spaces can be modified with the MORE /T option, but there is no way to preserve the TAB characters. Pass the file name (optionally with path) as the one and only argument.
#echo off
more %1 >%1.new
move /y %1.new %1 >nul
All that is needed is to read and echo each line. The FOR /F command is perfect, except it ignores empty lines. Here I use FINDSTR to prefix each line with the line number, followed by a :, thus preserving empty lines. Then I use search and replace to remove the number prefix. I must toggle delayed expansion on and off within the loop to preserve any ! that may appear. This script preserves TABs, but is limited to ~8191 bytes per line. It is also relatively slow. It will become very slow with very large files.
#echo off
setlocal disableDelayedExpansion
>%1.new (
for /f "delims=" %%A in ('findstr /n "^" %1') do (
set "ln=%%A"
setlocal enableDelayedExpansion
echo(!ln:*:=!
endlocal
)
)
move /y %1.new %1 >nul
Finally, here is a hybrid batch/JScript solution that is very fast, and does not have any limitations that I am aware of.
#if (#X)==(#Y) #end /* Harmless hybrid line that begins a JScript comment
::************ Batch portion ***********
#echo off
<%1 cscript //E:JScript //nologo "%~f0" >%1.new
move /y %1.new %1 >nul
exit /b
************* JScript portion **********/
while (!WScript.StdIn.AtEndOfStream) {
WScript.Stdout.WriteLine(WScript.StdIn.ReadLine());
}

Batch script that separates that creates folder by month

#ECHO OFF
SETLOCAL EnableDelayedExpansion
REM Set variables
SET SOURCE=C:\My WebEx Recordings
SET DEST=\\XXXRD12\c$\WebExVideoArchive
SET 7ZIP=C:\Program Files\7-Zip\7z.exe
REM Compress local files with 7zip
ECHO ---------------------------------------------------------
ECHO BEGINNING VIDEO COMPRESSION OPERATIONS
ECHO ---------------------------------------------------------
CD /D "%SOURCE%"
FOR %%f in ("*.wrf") DO (
SET FILENAME=%%~nf
ECHO Compressing !FILENAME!
"!7ZIP!" a -t7z -aoa "!FILENAME!.7z" "%%f"
)
REM Copy compressed files
ECHO ---------------------------------------------------------
ECHO COMPRESSION COMPLETE - BEGINNING COPY OPERATIONS
ECHO ---------------------------------------------------------
REM XCOPY <source> <destination> <options>
XCOPY "%SOURCE%\*.7z" "%DEST%" /Y /V /I /R
REM Confirm successful copy, then delete originals
IF %ERRORLEVEL% EQU 0 (
ECHO Copy Operation Successful. Removing Originals...
DEL /Q "%SOURCE%\*.*"
)ELSE (
ECHO Error Detected During Copying. Please try again...Press Any Key to Exit
Pause
)
I am trying to edit this code to when we run the batch file it creates a folder based off the month of our file format. We ran this script a whole lot but forgot to make a folder called April now we have May mixed in with April.
The files are formatted like
Username-R705-2011.05.04-1601-Disconnected.7z
I was wondering if there is anyway it can go off the .04 and make a folder for that month so it will automatically put it in the folder it is needing to go into, so it will be easy to search for by month.
** would it be possible if we can not use the format that we format our files in to have it sort by file creation.
Erase everything below (and including) the line REM XCOPY <source> <destination> <options> and replace it with the following:
for /F "usebackq delims=" %%a in (`dir /b "%SOURCE%\*.7z"`) do (
SET CURRENT_FILE=%%a
REM Extract the month.
for /F "usebackq tokens=3 delims=-" %%i in ('!CURRENT_FILE!') do (
SET CURRENT_FILE_DATE=%%i
SET FILE_MONTH=!CURRENT_FILE_DATE:~-2!
SET MONTH_DEST=!DEST!\!FILE_MONTH!
)
XCOPY "%SOURCE%\!CURRENT_FILE!" "!MONTH_DEST!\" /Y /V /I /R
REM Confirm successful copy, then delete original
IF %ERRORLEVEL% EQU 0 (
ECHO Copy Operation Successful. Removing Original...
DEL /Q "%SOURCE%\!CURRENT_FILE!"
)ELSE (
ECHO Error while copying "%SOURCE%\!CURRENT_FILE!.
)
)
This code goes through every file matching %SOURCE%\*.7z, extracts the month, and then copies the file to %DEST%\<month>. Month is just the 2-digit number from the filename.
Seems like you have a folder full of files with different numeric-month values embedded in the filename. I was thinking of extracting that value from each file, but it would be more straightforward to use brute force wildcards with 12 different XCOPY commands:
XCOPY "%SOURCE%\*-*-20??.??.01-*.7z" "%DEST%\01" /Y /V /I /R
XCOPY "%SOURCE%\*-*-20??.??.02-*.7z" "%DEST%\02" /Y /V /I /R
[...]
XCOPY "%SOURCE%\*-*-20??.??.12-*.7z" "%DEST%\12" /Y /V /I /R
I may be misunderstanding the question but if you can make some assumptions about the filenames, this would work.