write a batch script to find a sub directory - batch-file

I am new to batch scripting. I am to write a batch script which will search for a sub directory and then compare the extension of the files present inside the directory.
For example, for the following structure:
D:\Batch_script\unknown_dir_name\destiantion1\dummy.txt
My script should first find the directory destiantion1 and then if it exist it should look for dummy.txt file.
Is it possible? can anyone please suggest me a way to do this.
Thanks

for /f "tokens=*" %%f in (
'dir /ad /s /b "d:\Batch_script\destiantion1"'
) do if exist "%%~f\dummy.txt" echo %%~f
For each directory named destiantion1 under d:\batch_script, if it contains a dummy.txt file, echo to console
EDITED - to adapt to comments
for /f "tokens=*" %%f in (
'dir /ad /s /b "d:\Batch_script\destiantion1"'
) do if exist "%%~f\dummy.txt" (
dir /a-d /b "%%~f\dummy.txt" 2>nul | find "dummy.txt" > nul && echo %%~f
)
for and dir commands do not differentiate upper and lower case at search, so, use a filter. If the string is found, no errorlevel is set and the code after && is executed.
Other alternative is to process the complete list of files searching for the adecuated folder/file
for /f "delims=" %%f in (
'dir /a-d /s /b "d:\batch_script\dummy.txt" ^| findstr /l /e /c:"\destiantion1\dummy.txt"'
) do echo %%~dpf

Related

Strict string matching locate file batch - Case sensitive

I have a piece of code which runs through each line in a find.txt file and tries locate it. If it does not exist it will populate a output.txt file. Thing is, if a file is called "Egg.mp3" and in my find.txt has "egg.mp3" it counts that as if it found it? Now correct.. It did but i need something thats strict! Case sensitive even so that "Egg.mp3" is not the same as "egg.mp3" therefore to drop "egg.mp3" into my output.txt.
Does anyone have a solution to this? I searched around and found nothing that may help.
Batch code:
for /f "usebackq delims=" %%i in ("E:\find.txt") do IF EXIST "C:\Users\PC\Desktop\Lib\%%i" (echo "File Exists") ELSE (echo "C:\Users\PC\Desktop\Lib\%%i">> "C:\Users\PC\Desktop\output.txt")
pause
Windows does not differentiate case when dealing with file or folder names. So "egg.mp3" and "Egg.mp3" really are equivalent.
But if you still want to include file names that differ only in case, then you can do the following:
#echo off
set "folder=C:\Users\PC\Desktop\Lib"
set "output=C:\Users\PC\Desktop\output.txt"
pushd "%folder%"
>"%output%" (
for /f "usebackq delims=" %%F in ("e:\find.txt") do dir /b /a-d "%%F" 2>nul | findstr /xc:"%%F" >&2 || echo %folder%\%%F
)
popd
The following would be a lot faster (assuming you don't really need the path info in the output), but this nasty FINDSTR bug prevents the following from working properly - DO NOT USE!
#echo off
dir /b /a-d "C:\Users\PC\Desktop\Lib" >"e:\temp.txt"
findstr /LXVG:"e:\temp.txt" "e:\find.txt" >"C:\Users\PC\Desktop\output.txt"
del "e:\temp.txt"
If you have JREPL.BAT, then you can do the following instead:
#echo off
dir /b /a-d "C:\Users\PC\Desktop\Lib" >"e:\temp.txt"
call jrepl "e:\temp.txt" "" /b /e /r 0:FILE /f "e:\find.txt" /o "C:\Users\PC\Desktop\output.txt"
del "e:\temp.txt"
If you really need the path info in your output, then you can do the following:
#echo off
dir /b /a-d "C:\Users\PC\Desktop\Lib" >"e:\temp.txt"
jrepl "e:\temp.txt" "" /b /e /r 0:FILE /f "e:\find.txt" | jrepl "^" "C:\Users\PC\Desktop\Lib\" /o "C:\Users\PC\Desktop\output.txt"
del "e:\temp.txt"
Based off of a comment in this solution, this should do what you want:
#echo off
for /f "usebackq delims=" %%i in ("find.txt") do (
echo Checking for %%i...
dir /b /a-d "%%i"|find "%%i" >nul
if %errorlevel% == 0 (
echo "File Exists"
) ELSE (
echo "Not found"
)
)
Example of the base command in action:
D:\batch>dir /b /a-d "egg.mp3"|find "egg.mp3"
D:\batch>dir /b /a-d "Egg.mp3"|find "Egg.mp3"
Egg.mp3

Looping through directories to extract pages from multiple pdfs

I have multiple directories that contain one pdf in each. Im trying to extract page 1 from each pdf and keep it in the directory that the original pdf was in so i end up with the multiple directories now containing two pdfs.
I currently have this code using ghost scripts which works, but only if the pdfs are in the same directory as the batch file
echo on
for %%I in ("D:\files\input\" *.pdf) do "C:\Program Files\gs\gs9.20\bin\gswin64c.exe" -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="%%~nI_p1.pdf" -dFirstPage=1 -dLastPage=1 "%%I"
I cant see why all the directories in D:\files\input\ are not being looped through.
You are executing the command with two parameters,"D:\files\input\" and *.pdf.
You need to look for each *.pdf file. The accepted way is
for /f "delims=" %%I in ('dir /b /s /a-d "D:\files\input\*.pdf" ') do ...
directory, basic format, with subdirectories, but no directorynames.
However, this would execute the command against every *.pdf including the *_p1.pdfs (ie those already processed) so
for /f "delims=" %%I in ('dir /b /s /a-d "D:\files\input\*.pdf"^|findstr /v /i /e /L "_p1.pdf" ') do ...
would exclude those files by find those names that do not (/v) end (/e) with the literal (/L) "_p1.pdf" without regard to case (/i). It would however regenerate the _p1s.
for /f "delims=" %%I in ('dir /b /s /a-d "D:\files\input\*.pdf" ') do if not exist "%%~dp*_p1.pdf" ...
would process those *.pdfs that have not already been processed.
(not tested)
Your usage of the for loop is the problem. See for /?
Magoo was faster, see this complete solution.
#Echo off
Set "GS=C:\Program Files\gs\gs9.20\bin\gswin64c.exe"
Set "Opt=-dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dFirstPage=1 -dLastPage=1"
Pushd "D:\files\input\"
for /f "delims=" %%I in (
'Dir /B/S *.pdf ^|find /v "_p1.pdf" '
) do If not exist "%%~dpnI_p1.pdf" "%GS%" %Opt% -sOutputFile="%%~dpnI_p1.pdf" "%%~fI"
Popd

In batch, how to list files of a specific directory?

I need to go through all the directories except one (named "FORBIDDEN"), and to print for each of them all the files they contain.
So I wrote a batch script like this:
#echo off
for /f "tokens=*" %%G in ('dir /b /s /a:d %cd%') do ^
if %%G NEQ C:\Users\ME\FORBIDDEN (dir /a-d %%G)
But the part (dir /a-d %%G) is not good because I get some errors saying that files were not find.
So, for each round of the loop, how to list all files present in the directory (whose path is in %%G) ?
Cheers
for /d /r %%d in (*) do if not "%%~nxd"=="FORBIDDEN" 2>nul dir /a-d "%%d"
For each folder if it is not the excluded one, show its contents
edited to adapt to comments
To only include the files with full path
for /d /r %%d in (*) do if not "%%~nxd"=="FORBIDDEN" (
for %%f in ("%%~fd") do echo "%%~ff"
)
Another option (that also includes files in the current folder) could be
dir /a-d /s /b | find /v "\FORBIDDEN\"
Get the full list and filter it, to only retrieve the lines that does not make reference to the excluded folder

How to rename multiple folders that have a set number of files within that folder using batch script?

I am very new to programming so your help is much appreciated!
I have a top directory, containing subfolders which have files in them. I want to add the string "x" after the folder name e.g. foldername -> foldernamex if the subfolders contain less than 4 or greater than 4 files. How do I go about doing this?
Here is my attempt so far:
for %%e in ('dir ^| find "File(s)"') do (
set cnt=%%e echo File count = %cnt%
if File count <4 do ren "%%e" "%%~nxf_x"
)
This uses the dir command and the brief /b switch with the file-only switch /a-d and pipes the result through the find /c /v "" command to count the number of files.
findstr is used to find the exact numeral 4 and if it doesn't detect 4 then the script renames the folder and restarts the loop to handle nested subdirectories (because the folder structure changes when a folder is renamed).
It's not tested - so test it on a sample folder tree with copies of some folders first.
#echo off
:loop
for /f "delims=" %%a in ('dir /b /s /ad ^|findstr /v /i "x$" ') do (
dir "%%a" /b /a-d 2>nul |find /c /v "" |findstr "^4$" >nul || (ren "%%a" "%%~nxax" & goto :loop)
)
pause
Simpler code for processing only the folders inside the current folder.
#echo off
for /f "delims=" %%a in ('dir /b /s /ad ') do (
dir "%%a" /b /a-d 2>nul |find /c /v "" |findstr "^4$" >nul || ren "%%a" "%%~nxax"
)
pause

Partial path known..need to search for file type inside

There is a particular folder that begins with a name such as SS followed by random characters. The names would be different every time and the only thing we are sure of is the folder begins with SS. How do we look if this folder has a .txt file inside in batch programming.
An idea :
#echo off
for /f "delims=" %%a in ('dir /b/ad ^|find /i "SS"') do set $Dir=%%a
dir /b/a-d *.txt %$dir%>nul
if %errorlevel% equ 0 echo File(s) found in "%$DIR%"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%\ss*" 2^>nul'
) DO (
FOR /f "delims=" %%h IN (
'dir /b /a-d "%sourcedir%\%%a\*.txt" 2^>nul'
) DO (
ECHO "%sourcedir%\%%a\%%h"
)
)
GOTO :EOF
should solve your problem - you need to change sourcedir to suit your system, obviously.
The code below check if the folder contain any .txt file:
#echo off
set "filePath="
for /D %%a in (SS*) do if exist "%%a\*.txt" do set "filePath=%%a"
if defined filePath echo File exists in folder %filePath%
If you want to check for a particular .txt file, just change *.txt by the appropriate name.

Resources