How to apply my search on files with different extension in batch - batch-file

This batch code is to search all the PDF files in the path specified by the user and copy them all in the home folder and then deleting everything that is inside that folder (subfolders and files). How can I force my code to look for multiple files like pdf,txt and else.
#echo off
setlocal enabledelayedexpansion
goto :main
:main
setlocal
cls
echo.
echo Enter the home directory path where you want to apply the cleaning
set /p path=
echo %path%
cd %path%
for /d %%g in (*) do (
echo %%g
cd %%g
for /r %%p in (*.pdf) do (
set dest=!cd!
set app=/
copy %%p !%dest%%app%!
echo %%p
echo !cd!
)
for /d %%z in (*) do (
rmdir %%z /s /q
)
cd ..
)
pause
endlocal
goto :eof

Nothing prevents you from doing
for /r %%p in ( *.jpg *.png ) do (
REM do something with %%p
)
The catch is that you can use any number of extensions separated by space withing parenthesis.

Related

Move files to parent directory if their extension matches the sub-directory extension

I am looking for a batch script which will move files from a sub-directory to its parent if their extension matches the sub-directory extension.
Examples:
Move any .txt file from directory parent\files.txt\
"parent\files.txt\test.txt" will become "parent\test.txt"
Move any .zip file from directory parent\files.zip\
"parent\files.zip\test.zip" will become "parent\test.zip"
I want only to move the file if its extension is the same as that of its sub-directory name. The sub-directory and any other content has to be left alone.
This is what I have now, but it only removes my files:
#echo off
md temp
set view=
if "%view%"=="1" #echo on
color 72
mode con cols=30 lines=8
setlocal enableDelayedExpansion
set /p location_with_dirs=location:
echo:
type nul> ".\temp\folderlist.txt"
FOR /D %%G IN ("%location_with_dirs%\*") DO (
echo process file:
echo %%G
choice
if "%errorlevel%"=="1" echo %%G >> ".\temp\folderlist.txt"
cls
)
FOR /f "delims=" %%G in (".\temp\folderlist.txt") DO (
for %%F in (%%G\*.*) do move /Y %%F "%location_with_dirs%\"
rd %%G
)
del /f /q ".\temp\folderlist\*.txt"
I'm sure there will be many ways of achieving this, here's one:
#CD /D "Parent" 2>Nul || Exit /B
#For /D %%A In (*) Do #If /I Not "%%~xA"=="" Move /-Y "%%A\*%%~xA">Nul
Just change Parent on line 1 to the full or relative path of your parent directory.
Just as a courtesy, heres a version which hopefully improves on the more important part of your provided code, (it ignores the progress bar and color stuff):
#Echo Off
:AskDir
Set "dir_with_files="
Set /P "dir_with_files=location: "
If "%dir_with_files%"=="" GoTo AskDir
If "%dir_with_files:~-1%"=="\" Set "dir_with_files=%dir_with_files:~,-1%"
If Not Exist "%dir_with_files%\" GoTo :AskDir
Set "_rand=%dir_with_files%\%random%.organizer.lock.txt"
Type Nul>"%_rand%" 2>Nul && (Del "%_rand%") || GoTo :AskDir
If "%dir_with_files:~-1%"==":" Set "dir_with_files=%dir_with_files%\"
CD /D "%dir_with_files%" 2>Nul || Exit /B
For /D %%A In (*) Do If /I Not "%%~xA"=="" Move /-Y "%%A\*%%~xA">Nul 2>&1
Exit /B.

Iterate Directories with Batch

The objective is to display the directory name in MYFOLDER.
MY.exe exists in the folder, but curiously, without the wildcard in ...\desktemp*, the "#echo Showing subfolders" is never displayed, but "#echo G is working" is. However MY.exe is never found when moved to one of the subfolders.
OTOH the current code never finds MY.exe and never displays "#echo G is working" but properly lists each subfolder: "#echo Showing subfolders".
The other problem is the pauses at the end of the block are never reached.
Substituting the inner For with
cd \Users\%USERNAME%\Desktop
for /D /r %%G in ("desktemp*") do (
gets essentially the same result. My.exe isn't found if moved to one of the subfolders of desktemp.
Setlocal EnableDelayedExpansion
set CURRDRIVE=C
SET MYFOLDER=
:SEARCHDRIVES
REM BLOCK
for %%B in (C) do (
if exist %%B: (
PUSHD %%B:\
if NOT DEFINED MYFOLDER (
ECHO "%CD%"
REM This always displays path batch is run from.
REM The above Pushd doesn't change to C:\
for /f "tokens=*" %%G in ('dir /b /s /a:d "%%B:\Users\%USERNAME%\Desktop\desktemp*" ^| find "\"') do (
#echo Showing subfolders
#echo %%G
pause
if exist "%%G\MY.exe" (
call set MYFOLDER=%%G
#echo %%G
#echo G is working
call echo %MYFOLDER%
pause
GOTO GOTMYFOLDER
)
)
)
)
REM Exist Drive
)
REM Drives Loop
:GOTMYFOLDER
cd /d %CURRDRIVE%:\
echo %MYFOLDER%
cd %MYFOLDER%
pause
The above is a chunk whittled from a larger code block: the ultimate aim will be to get the folder names"\Users\New\Desktop\desktemp" into a variable via prompt.
Are the Escape Characters, Delimiters and Quotes in the nested blocks implemented properly?
The answer escaped this poor little brain until it cottoned on to what the "DIR" and "For /D /R" were really up to. What was sought for was in the addition of a new "For /D" (no /R).
This first (extra) "For /D" determines the folder names to iterate from.
(Specifically anything but the Windows directory where we run into problems with >260 filenames.)
This locates the MY.exe file somewhere in the Users folder (more precisely in any root folder beginning with U):
Setlocal EnableDelayedExpansion
set CURRDRIVE=C
SET MYFOLDER=
:SEARCHDRIVES
REM BLOCK
for %%B in (C) do (
if exist %%B: (
PUSHD %%B:\
if NOT DEFINED MYFOLDER (
ECHO "%CD%"
REM This always displays path batch is run from.
REM The above Pushd doesn't change to C:\
for /D %%Z in (U*) do (
cd \%%Z
for /D /r %%G in ("*") do (
if exist "%%G\MY.exe" (
call set MYFOLDER=%%G
#echo %%G
#echo G is working
call echo %MYFOLDER%
pause
GOTO GOTMYFOLDER
)
)
)
)
)
REM Exist Drive
)
REM Drives Loop
:GOTMYFOLDER
cd /d %CURRDRIVE%:\
echo %MYFOLDER%
cd %MYFOLDER%
pause
Edit:
The source of the error spam in the comment below is this command:
Insert batch code to elevate UAC privileges [code][1] from TanisDL
Setlocal EnableDelayedExpansion & pushd "%CD%" & CD /D "%~dp0"
set CURRDRIVE=C
FOR /F "usebackq delims==" %%G IN (dir %CURRDRIVE%:\ /A:D /O:G /S /B ^| FIND /I "myString") DO (set "foundMyString=%%~pG")

Batch Rename rightmost 3 characters

This is not working.. No errors.
Have directories:
123.abc
123.def
123.ghi
Want to rename to:
abc
def
ghi
What I have done is not working..
I have used: http://www.dostips.com/DtTipsStringOperations.php
Renaming Folder Structure in Batch
SETLOCAL ENABLEDELAYEDEXPANSION
for /d %%D in ("C:\batch\*") do CALL :RENAME %%D %%~nxD
:RENAME
set "folder=%%~nxD"
rem https://stackoverflow.com/questions/11040473/batch-file-string-character-split
set "x=%folder:~-3%"
FOR /D %%R IN (%1%) DO RENAME %%R "%x%"
ENDLOCAL
pause
#echo off
rem Prepare environment
setlocal enableextensions enabledelayedexpansion
rem For each directory under selected folder
for /d %%d in ("c:\batch\*") do (
rem Get the extension of the directory if any
set "name=%%~xd"
rem If there is a extension
if defined name (
rem Remove the dot from extension
set "name=!name:~1!"
rem If no file/folder exists with the new name, rename the dir
if not exist "%%~dpd\!name!" echo ren "%%~fd" "!name!"
)
)
endlocal
Final rename command is "echoed" to the console. If output is correct then remove the echo from ren command line.
This will rename the folders to the text after the first period.
Remove the echo to activate the command as currently it will only echo the commands to the console.
#echo off
for /f "tokens=1,* delims=." %%a in ('dir "C:\batch" /ad /b ') do echo REN "C:\batch\%%a.%%b" "%%b"
pause

Batch file: sorting folders and files alphabetically

The batch file below recursively echos files and folders while adding some simple formatting, such as indenting to show the recursion depth, adding "/ " before folder names, "*" before certain files, and skipping folders named "Archive". It works great except that files and folders are sorted randomly, rather than alphabetically. How could this be changed to sort both files and folders alphabetically?
#echo off
setlocal disableDelayedExpansion
pushd %1
set "tab= "
set "indent="
call :run
exit /b
:run
REM echo the root folder name
for %%F in (.) do echo %%~fF
echo ------------------------------------------------------------------
set "folderBullet=\"
set "fileBullet=*"
:listFolder
setlocal
REM echo the files in the folder
for %%F in (*.txt *.pdf *.doc* *.xls*) do echo %indent%%fileBullet% %%F - %%~tF
REM loop through the folders
for /d %%F in (*) do (
REM skip "Archive" folder
if /i not "%%F"=="Archive" (
REM if in "Issued" folder change the file bullet
if /i "%%F"=="Issued" set "fileBullet= "
echo %indent%%folderBullet% %%F
pushd "%%F"
set "indent=%indent%%tab%"
call :listFolder
REM if leaving "Issued folder change fileBullet
if /i "%%F"=="Issued" set "fileBullet=*"
popd
))
exit /b
Very little change required. Convert FOR loops to FOR /F running sorted DIR commands. The /A-D option lists files only, and /AD lists directories only.
This version sorts files by name
#echo off
setlocal disableDelayedExpansion
pushd %1
set "tab= "
set "indent="
call :run
exit /b
:run
REM echo the root folder name
for %%F in (.) do echo %%~fF
echo ------------------------------------------------------------------
set "folderBullet=\"
set "fileBullet=*"
:listFolder
setlocal
REM echo the files in the folder
for /f "eol=: delims=" %%F in (
'dir /b /a-d /one *.txt *.pdf *.doc* *.xls* 2^>nul'
) do echo %indent%%fileBullet% %%F - %%~tF
REM loop through the folders
for /f "eol=: delims=" %%F in ('dir /b /ad /one 2^>nul') do (
REM skip "Archive" folder
if /i not "%%F"=="Archive" (
REM if in "Issued" folder change the file bullet
if /i "%%F"=="Issued" set "fileBullet= "
echo %indent%%folderBullet% %%F
pushd "%%F"
set "indent=%indent%%tab%"
call :listFolder
REM if leaving "Issued folder change fileBullet
if /i "%%F"=="Issued" set "fileBullet=*"
popd
))
exit /b
To sort by extension first, then by name, simply change /ONE to /OEN.
Try changing your for /d loop from
for /d %%F in (*) do
to
for /f "delims=" %%F in ('dir /b /o:n *.') do
and see whether that makes a difference. Actually, ordering by name is the default behavior for dir, so you could probably get away with
for /f "delims=" %%F in ('dir /b *.') do
If some of your directory names have dots in them, you'll need to change it a little.
for /f "delims=" %%F in ('dir /b') do (
rem Is this a directory?
if exist "%%F\" (
rem do your worst....
)
)

Replace text in .bat file

I have 2 folders in folder called C:\durvi\mmi_test\mmidurvi which are
C:\durvi\mmi_test\mmidurvi\durvyauu
C:\durvi\mmi_test\mmidurvi\sgdf
Both these folders have Connections.xml file
I would like to replace any occurance of ql99015 with dd32261
A sample format of file is as below
<pre><anyType xsi:type="xsd:string">ql99015</anyType>
<anyType xsi:type="xsd:string">ql99015_flowreeng_Anthony</anyType> </pre>
I tried something like below but does not work:
for /D %%f in (c:\durvi\mmi_test\mmidurvi\*) do (
cd %%f
if not exist "Connections.xml" (echo this file does not exist)&goto :eof
SETLOCAL=ENABLEDELAYEDEXPANSION
ren "Connections.xml" "Connections1.xml"
for /f %%a in (Connections1.xml) do (
set write=%%a
echo %%a
if !write!=="ql99015" set write="dd32261"
echo !write! >> Connections.xml
)
del "Connections1.xml"
cd..
)
Thanks for your help in advance!
Change the script to:
SETLOCAL ENABLEDELAYEDEXPANSION
for /r %%a in (connections.xml) do (
move "%%a" "%%a.temp"
for /f "usebackq tokens=*" %%b in ("%%a.temp") do (
set write=%%b
echo !write:ql99015=dd32261! >> "%%a"
)
del "%%a.temp"
)

Resources