a 'search and extract' batch file - batch-file

I'd like to ask your assistance in the following batch script:
- Search for a specific file in all drives
- If found extract an archive to that directory
Thank you in advance
Edit code added from the comments area
for /f "delims=" %%F in ('dir /b /s "C:\test.txt" 2^>nul') do set place1=%%F
for /f "delims=" %%F in ('dir /b /s "C:\copy.rar" 2^>nul') do set place2=%%F
unrar x place2\copy.rar *.gif place1

Related

Get subdirectories with specific prefix

I'm creating simple batch script for deleting all user configs of specific application and I'm still failing on the last step where I'm trying to get all subfolders with specific prefix...
This is what I have right now:
#echo off
chcp 1250
SET appUserConfigDirectory=\AppData\Local\CompanyName
SET appConfigFolderPrefix=AppName.exe_Url
:: get parent folder of user folders
for %%d in (%USERPROFILE%) do SET userprofilesFolder=%%~dpd
SETLOCAL ENABLEDELAYEDEXPANSION
:: going through all user folders
for /F "delims=" %%d in ('dir %userprofilesFolder% /A:D-R-H-S /b') do (
:: set full name of CompanyName folder in user AppData
SET appConfigParentFolder=%userprofilesFolder%%%d%appUserConfigDirectory%
IF EXIST !appConfigParentFolder! (
:: There is a problem with dir command, it's says File not found even if subfolder with this prefix exists and print all subFolder no metter it's name...
for /F "delims=" %%i in ('dir !appConfigParentFolder! /A:D /b %appConfigFolderPrefix%*') do (
echo %%i)))
I find the way how to do it. The right dir command with prefix should be:
dir !appConfigParentFolder!\%appConfigFolderPrefix%* /A:D /b
So the full version of this sample is:
#echo off
chcp 1250
SET appUserConfigDirectory=\AppData\Local\CompanyName
SET appConfigFolderPrefix=AppName.exe_Url
for %%d in (%USERPROFILE%) do SET userprofilesFolder=%%~dpd
SETLOCAL ENABLEDELAYEDEXPANSION
for /F "delims=" %%d in ('dir %userprofilesFolder% /A:D-R-H-S /b') do (
SET appConfigParentFolder=%userprofilesFolder%%%d%appUserConfigDirectory%
IF EXIST !appConfigParentFolder! (
for /F "delims=" %%i in ('dir !appConfigParentFolder!\%appConfigFolderPrefix%* /A:D /b') do echo %%i
)
)
Please try with something like this:
forfiles /S /M !appConfigParentFolder! /C "cmd /c if #isdir==TRUE rmdir #path"
It removes every subdirectory, which equals !appConfigParentFolder!.

Create BAT file to get directory listing up to 2nd level only

I have a window directory with 300 folders inside of it and 4 levels of folders within each folder. I need a listing of folders only up to 2nd level in a txt file? I need the full path of each directory as well. I would like to use this a BAT file or CMD line prompt. Ex:
Jon Done
Test001
Tester002
Test003
Tester004
Can anyone help me to do this?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
PUSHD "%sourcedir%"
FOR /f "delims=" %%a IN ('dir /b /a-d 2^>nul') DO ECHO %%~fa
FOR /f "delims=" %%a IN ('dir /b /ad') DO (
PUSHD "%%a"
FOR /f "delims=" %%x IN ('dir /b /a-d 2^>nul') DO ECHO %%~fx
popd
)
popd
GOTO :EOF
This should work - you would need to change the setting of sourcedir to suit your circumstances.

Get inverse list of files through dir command in batch

I'm triying to get a list of files of subfolders in a inverse order using this:
for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f)
I'm getting this result
folder1\file2, folder1\file, folder2\file2, folder2\file1, folder3\file2, folder3\file1
And I want to get the reverse order in folders and files, not only files of folders. Something like this
folder3\file2, folder3\file1, folder2\file2, folder2\file1\, folder1\file2, folder1\file1
How can I do this?
#echo off
for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f >> tempfile.123)
C:\Windows\System32\sort.exe /R tempfile.123
del tempfile.123
This just echoes your files to tempporary file and then revereses it. Sorry for the full path to sort.exe but I have cygwin installed. In case you want proper temporary file name I reccomend this http://unserializableone.blogspot.com/2009/04/create-unique-temp-filename-with-batch.html
try this
for /f "tokens=*" %%f in ('dir /s /b /o-n')
do (
SET OUTPUT=%OUTPUT%, %%f
)
echo %OUTPUT%
run a first loop to get first level subdir then run you command on each of them
#echo off
for /f "tokens=*" %%f in ('dir c:\temp\so\ /b /o-n /AD') do (call :filesOf %%f)
:next
echo next
pause
:filesOf
echo "files for ******** %1 *********"
if ("%1"=="") goto next else(
for /f "tokens=*" %%i in ('dir c:\temp\so\%1 /s /b /o-n ') do echo "***%%i***"
)
it will be difficult to handle multi level subdirs tough

Batch Search combo Z: Test List

for /f "tokens=* delims=" %%a in ('dir "recordings.txt" /s /b') do (
echo %%a
)
Is this the correct format to look on the Z: drive for files in the recordings.txt?
Tried powershell don't have permissions on the server
Contents of the file just have the file name / extension
3030009948_3030009912_df1389947f0fb80d62832122.sasf
The Directory structure of Z: is as follows
MM\dd\hh\mm\
recordings.txt is on the Desktop of my userprofile
I also need the paths of the found files
This should read recordings.txt on your desktop and create recordings-results.txt in the same place with the full path to every file inside it.
#echo off
dir /b /s /a-d "z:\" >"%temp%\results.tmp"
del "%userprofile%\desktop\recordings-results.txt" 2>nul
for /f "usebackq delims=" %%a in ("%userprofile%\desktop\recordings.txt") do (
echo finding "%%a"
findstr /i /c:"%%a" "%temp%\results.tmp" >>"%userprofile%\desktop\recordings-results.txt"
)
del "%temp%\results.tmp"
pause
This should be even faster:
#echo off
dir /b /s /a-d "z:\" >"%temp%\results.tmp"
findstr /i /g:"%userprofile%\desktop\recordings.txt" "%temp%\results.tmp" >"%userprofile%\desktop\recordings-results.txt"
del "%temp%\results.tmp"
pause
Dir is to look into a directory if you want to get a value from a file use :
for /f "tokens=*" %%a in ('type "Z:\recordings.txt"') do (
echo %%a
)

How can I copy files to the "newest subdir" in a directory using a batch file?

I need to copy files from recent build folder to another folder used for testing. I'm having a hard time getting the name of the most recent build folder.
My current attempt is this:
#for /D %%i in ('dir e:\builds\projectA\* /O:D') do set target=%%i
echo %target%
xcopy "%target%\*.*" \\devbox\projectA /y /s
I was hoping target would be the newly created folder from which I could then copy the files from. However, when I echo target to the console it just says:
/O:D'
Does anyone know how I can get this to work (or know of an alternative)?
Replace the /D with /F and add /B to the bracketed dir command.
#for /F %%i in ('dir e:\builds\projectA\* /O:D /B') do set target=%%i
echo %target%
xcopy "%target%\*.*" \\devbox\projectA /y /s
pushd E:\builds\projectA
for /f "delims=" %%d in ('dir /b /a:d /o:d') do #echo %%d>latest.txt
for /f "delims=" %%l in (latest.txt) do xcopy "%%l\*.*" \\devbox\projectA /y /s
del latest.txt
popd

Resources