How to call a batch file from batch file - batch-file

I need the syntax to call a batch file from the first batch file. The second batch files name changes with the revision. so i have only half of my second batch file name.
How do i search in a particular folder and call the second batch file..`?

Don't know the revision prefix or suffix, but you could try something like this:
for /f "tokens=1" %%n in ('dir /on /l /b /a-d "bat_file_*.bat"') do set latest_bat_file=%%n
It's relying on dir /on to sort by name, so it puts the last entry alphabetically in the variable %latest_bat_file%. You can then call it with:
call "%latest_bat_file%"
This assumes there are no spaces in your bat file names and that the revision is a numeric or alphabetical suffix. If you're using numbers, to avoid sorting problems, prefix your revision names with zero's (e.g. bat_file_001, bat_file_002).

FOR /R <path> will walk the directory tree for you.
FOR /R "%DIR_TO_SEARCH%" %%b IN (matching_*.bat) DO cmd /c "%%~b"
cmd /c will create a new shell instance, which means that if the invoked .BAT file sets environment variables, they won't be changed in calling script. This is usually what people want. If you actually wanted those side effects preserved, you could use call "%%~b" instead.

Related

How to delete long name folders under subfolder using batch file

I am trying to delete the sub folders using batch file.
I have folder
c:\users\mis
c:\users\mis\A\Third Party Log
c:\users\mis\B\Third Party Log
c:\users\mis\C\Third Party
c:\users\mis\D\Third Party
Etc.
I want to delete all "Third Party Log" folder and I tire as below:
#echo off
Set MainFolder=C:\users\mis
FOR /D %%D IN ("%MainFolder%\*") DO CALL :RENAME %%D
pause & exit
REM -------------------------------------------------------
:RENAME
Set CRITERIA=Third Party Log
FOR /D %%R IN ("%1\%CRITERIA%") do #if exist "%%R" rd /s /q "%%R"
REM -------------------------------------------------------**
If the folder is only Third it is deleting. Please help me deleting the fodlers.
Thanks,
Htet
#ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "targetdir=u:\your files"
SET "selectname=Third Party Log"
FOR /f "delims=" %%a IN (
'dir /b /s /ad "%targetdir%\%selectname%" '
) DO (
IF /i "%%~nxa"=="%selectname%" ECHO RD /S /Q "%%a"
)
Simply perform a dir /b /ad to list all of the directorynames matching the mask %selectname% and remove all of the hits.
Note that the for loop could be condensed to a single line, but I've split it over a number for clarity.
Naturally, the variables could be substituted by their literal values as well, if desired. This would reduce the entire batch to a single line which could be entered directly at the prompt in which case each %% needs to be reduced to a single %.
Posted code simply echoes the proposed instruction to allow for verification. I the displayed instructions are correct, remove the echo keyword to actually destroy the unwanted directories.
Finally, in your original code, rename is not a good name for the subroutine for two reasons : First, it is misleading as no renaming is being performed and second, ren is an alias of rename - and it's not a good idea to have a routine or variable or label that is itself a command-name.
I can sadly not write code and test it rn but:
You could use the dir command into a temp file.
Use findstr(or find i m not shure) and look for third party log.
Write this back to the temp file.
Now you should be left with a file filled with the paths of the folders you want gone.
Read the file line by line and delete and use this line string to provide the path for the del command.
To change the name as in the sample code you could take the line string and use it as the path to the ren command.
This is most likely a very bad approach but it should work.

Delete semi duplicate files

I'm wondering if there is a way to remove semi-duplicate files (name based) using a batch file or any other means (freeware utility) in Windows?
To give an example I have following files in a directory:
fileNameXXX(aaa).ext
fileNameXXX(bbb).ext
In this case, I only want to keep the fileNameXXX(bbb).ext
it's a single line in batch:
for /f "delims=" %%f in ('dir /b "*(*).ext" ^| find /v "(ddd)"') do ECHO del "%%f"
For every file matching the filemask excluding files with (ddd) do: delete it.
Remove the ECHO if the output fits your needs.
Note: if you want to use it directly on command line (instead in a batch file), replace every %%f with %f.
Tip: think about using some more code to check, if there is a Dutch version, and if not, keep the English one (or whatever you prefer).

shift file names with a batch file

Here is the file name format. The leading number is the layer and the second number is the material (3D printer).
01118_7.tif,
01118_6.tif,
01118_5.tif,
01118_4.tif,
01118_3.tif,
01118_2.tif,
01118_1.tif,
01118_0.tif
What I need to do is shift the files ending in _1, _4, _6 six places higher. So, 01124_1, 01124_4, 01124_6 while the rest of the files stay the same. I need to do it all the way down to layer 00112_*.
I'd like to do this via a batch file if I can. Was trying to follow a guide but the name format is tripping me up.
Basic excel format
I can't tell if you need to modify file names that appear within a text file, or if you need to rename files. Either way, I have a simple solution using one of two hybrid JScript/batch regex utilities:
Modify filenames within a text file using JREPL.BAT:
jrepl "^\d{5}(?=_[146]\.tif)" "lpad(Number($0)+6,'00000')" /i /j /f test.txt /O -
Rename files within the current directory using JREN.BAT:
jren "^\d{5}(?=_[146]\.tif$)" "lpad(Number($0)+6,'00000')" /i /j
Use call jrepl or call jren if you put the command within a batch script.
It took me awhile to understand that "shift file names six places higher" really means "add six to file name".
#echo off
setlocal EnableDelayedExpansion
set "numbers=/1/4/6/"
for /F "tokens=1,2 delims=_." %%a in ('dir /B /A-D *.tif') do (
if "!numbers:/%%b/=!" neq "%numbers%" (
set "newNum=1%%a+6"
ECHO ren "%%a_%%b.tif" "!newNum:~1!_%%b.tif"
)
)
If the names are not in files, but are lines of text placed in a text file, change the 'dir /B /A-D *.tif' command by the name of the text file.

How do I copy a directory that has a date stamp

I'm trying to copy the contents of a directory using a DOS batch file that begins with the computer name followed by an underscore and a date stamp. My first impulse was some variation of:
copy D:\%Computername%_\*\\*.* C:\WhateverPath
Of course I could not get this to work. Seems like a simple problem but I don't have much experience with batch files or DOS.
Try:
FOR /d %d IN (D:\%COMPUTERNAME%_*) DO xcopy %d C:\WhateverPath /E
This iterates over all directories (hence the /d) with the pattern %COMPUTERNAME%_* under D:\, and copies the contents of these directories into C:\WhateverPath. /Eis for copying all files and directories, also the empty ones.
For documentation of xcopy, type xcopy /? in a DOS shell (cmd).
Note: If you put this in a batch-file (something.bat), you must replace %d with %%d in the code above.
If you have multiple folders labeled C:\%computername%_%random_time_stamp%\ and you need to access each of them then move all of their contents to a single folder, you can do this:
Given the only underscore in the path is the one between %computername% and your timestamp
FOR /F "USEBACKQ tokens=*" %%F IN (`DIR /b /a:d "C:\" ^| FIND /I "%computername%_"`) DO (
COPY /y "%%~fF\*" "C:\WhateverPath\"
)
That states for every result that comes from the command DIR, /b switch meaning no header information, /a:d meaning only returning directories, I want to find only folders with the computername_ in it, and I want to copy the contents of each of those folders to C:\WhateverPath\ folder.

Trying to process files using a Batch. No output, Nothing happens

I'm trying to pass files one by one(I have to dot that since executable only accepts one file at a time). So, in my batch I have follwoing:
FOR /F %file IN ('dir /b /s *.css') DO CALL myExecutable.exe %file
I should see out files in same directory but nothing happens, no errors are displayed either.
Am I missing something here?
You have several mistakes in your example:
FOR parameter name is a single letter only
CALL is used to call another batch file or a subroutine in the existing batch file, not executables
the FOR parameter should be referenced with two %, when in batch file
you need to use a non-space delimiter, if the directory you run this command in or any subdirectory, or if any of the files has a space in the name
With these in mind, here's the right command you should be using:
for /f "usebackq delims=|" %%f in (`dir /b /s *.css`) do myexecutable.exe "%%f"
Here's my answer to a similar SO question, where I give more details on using FOR to process all files in a directory.

Resources