Run bat cmd in every sub-folder fail - for-loop

I need to run a bat file to rename the photo in each sub-folder as the sub-folder name and copy all the photos to a new folder. Each sub-folder contains only one photo.
I would like to write a batch file to do this task as there are many sub-folders, however, I can only successfully copy the photo in the last sub-folder.
The sub-folders are naming by number sequence starting from "00000001".
I found that the photos are renamed as the same thus only one photo can be copied.
Here's my code:
md "D:\photo"
for /r %%d in (.) do (cd %%d
for /r %%* in (.) do (set CurrDirName=%%~n*
echo %CurrDirName%
ren "*.jpg" "%CurrDirName%.jpg"
copy *.jpg "D:\photo"))
Please advice so that I could modify my code and do what I would like to do successfully, thanks a lot!
EDIT with more details
In cmd line, for example the program are running in the sub-foler "00000127"
set CurrDirName=00000127
but echo the %CurrDirName%, shows 00000128 which is the last sub-folder and every sub-folder return 00000128 but not the CurrDirName

Try this & remove the echo if the output is ok.
#echo off &setlocal
set "destination=d:\photo"
md "%destination%" 2>nul
for /d /r %%d in (*.*) do (
pushd "%%d"
for %%i in (*.*) do echo copy "%%~i" "%destination%\%%~nd.jpg"
popd
)
endlocal
There must be only one photo in each subfolder!

Related

How do I output the results of a batch file into a folder with the same name as the input file?

I am currently using a batch file which uses a programm called CriPackTools to extract the contents of a CPK file.
#echo off
for /r %%i in (*.cpk) do "CriPakTools.exe" "%%i" ALL
as you can see here it uses any cpk i drag onto the batch file and extracts the contents to the same directory.
Now how can I change it so it uses my XXXX.cpk, creates a folder named XXXX (the same as the file) and extracts its contents inside the folder.
Thanks
Assuming that the CriPakTools.exe extracting files into a current directory:
#echo off
for /r %%i in (*.cpk) do (
md "%%~dpni"
pushd "%%~dpni"
"CriPakTools.exe" "%%i" ALL
popd
)
Since you are using for /R loop, you are looping through all subfolders. If a .cpk file is located in a folder different than the %cd% or the location of CriPakTools.exe, then the command will fail. I would suggest the following code:
#echo off
pushd "C:\path\with\cpks"
for /R "C:\path\with\cpks" %%A IN (*.cpk) do (
cd "%%~dpA"
mkdir "%%~nA"
cd %%~nA
"CriPakTools.exe" "%%~fA" ALL
)
cd "C:\path\with\cpks"
echo The operation has finished.
pause>nul
exit /b 0
This, may help you.

How to exclude file extensions in the output filelist.txt, within a batch "dir" script in CMD

I am a CMD newbie and have a question with a batch script I am working on.
I have a parent directory with 30 sub-directories containing .pdf files, and I need a filelist.txt for each sub-directory, and have each filelist.txt save as the file name of the sub-directory it belongs too. This has been completed with the script below:
#echo off
cd /d "C:\Desktop\parentDir"
for /d %%a in (*) do (
DIR /B /ON /A-D "%%a" > %%a.txt.
move %%a.txt "%%a" >nul
)
My question is how, can I remove file extensions in the output of each filelist.txt. For ex. when I run the script now, the output .txt file shows 1111.pdf
1112.pdf
I need the ".pdf" removed
I know with a "for" command you can "do" echo %%~na to remove file extensions, but I have no clue how/where to factor this into the current script.
Any help is appreciated!
You are using DIR command to list all files. It does not have a switch to hide extension.
You can replace the DIR command with a FOR loop, like you pointed out.
cd /d "C:\Desktop\parentDir"
for /d %%a in (*) do (
for %%f in ("%%a\*") do #echo %%~nf >> %%a.txt.
move %%a.txt "%%a" >nul
)

Moving files from Subfolders into root directory, but not copying them to the next directory when ran again

I have a folder that contains subfolders with MP4 files. I'm trying to write a script that will move the MP4 files out of the subfolders into the root folder when ran. The batch file I wrote is working, but when the batch script runs again for new subfolders, the MP4 files that were already copied to the root folder, get moved up another level in the file structure. For example:
C:\MainRoot\Root\Subfolder\media.mp4
When script is ran, 'media.mp4' gets moved up to C:\Root\media.mp4 as desired.
But since I need the script to run on a scheduled task. The next time the script runs I get the following:
C:\MainRoot\media.mp4
Instead of just the MP4 file staying in C:\MainRoot\Root.
Here's my batch file so far to copy the mp4 files:
set root_folder=C:\MainRoot\Root
for /f "tokens=1* delims=" %%G in ('dir %root_folder% /b /o:-n /s ^| findstr /i ".mp4" ') do (
move /y "%%G" "%%~dpG..\%%~nxG"
)
What do I need to modify so that once moved, the MP4 files will stay in place?
Any help would be greatly appreciated!
Since all your source files seem to be at a certain directory level, a for /D loop could be wrapped around your for /F loop, which parses the output of a non-recursive dir command line (no /S):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=C:\MainRoot\Root"
set "_PATTERN=*.mp4"
rem // Loop through sub-directories:
for /D %%D in ("%_ROOT%\*") do (
rem // Loop through matching files:
for /F "eol=| delims=" %%F in ('dir /B "%%~fD\%_PATTERN%"') do (
rem // Avoid overwriting destination file:
if not exist "%_ROOT%\%%~nxF" (
rem // Move matching file one level up:
move /Y "%%~fD\%%~nxF" "%_ROOT%\%%~nxF"
)
)
)
endlocal
exit /B
If you are happy to overwrite as in your provided example then something as simple as this may suit your purpose:
#Echo Off
Set root_folder=C:\MainRoot\Root
If /I NOT "%CD%"=="%root_folder%" PushD "%root_folder%" 2>Nul||Exit/B
For /R %%G In (*.mp4) Do If /I NOT "%~dpG"=="%root_folder%\" Move "%%G">Nul 2>&1
If the files are only one folder deep you may prefer this:
#Echo Off
Set root_folder=C:\MainRoot\Root
If /I NOT "%CD%"=="%root_folder%" PushD "%root_folder%" 2>Nul||Exit/B
For /D %%G In (*) Do Move "%%G\*.mp4">Nul 2>&1

Searching for string in multiple zip archives

Here's what I have;
On a windows-based web server there are roughly 1,000 zip files, each with dozens of log files inside. I already have a script that goes through each archive and deletes all but one specific file type (in an attempt to save diskspace and delete things I don't need). Then, the script unzips each archive to their own folder. And I know how to code the reverse of that to zip them back when I'm done.
Here's what I need to figure out;
Once I run the previously mentioned script (we call it garbageman because it cleans out the garbage in the zip files) I need to go through the remaining 5 or 6 files in each of the newly created unzipped folders, and look for a specific string in each file. If I find the string, I delete everything that is not that string, and save it to a file called "export.txt" in that folder. Then, I move to the next unzipped file, and so on. Once completed, I need re-zip everything back together into their own archives
Here's what I have for code so far. Any help is extremely appreciated.
cd "C:\Program Files\7-Zip"
FOR %%c in (C:\Users\xxxxxx\Desktop\LogQueue\*.*) DO 7z d %%c "-x!xstore*" -r
FOR /R "C:\Users\xxxxxx\Desktop\LogQueue" %%I in ("*.zip") do (
"%ProgramFiles%\7-Zip\7z.exe" x -y -o"%%~dpnI" "%%~fI"
)
cd "C:\Users\xxxxxxx\Desktop\LogQueue"
FOR /R "C:\Users\xxxxxx\Desktop\LogQueue" %%I in ("*.*") do (
findstr "xxxxxxxx_eReceipt" %%~fI > %%~dpnI\export.txt
pause
)
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X\"
This is an edit:
This script should do what you want.
Only Change sourcedir and mystring variables.
export.txt will be inside a file called Storage in the root directory of the batch.
:ScriptA
#ECHO ON
MKDIR "%CD%\Storage"
MKDIR "%USERPROFILE%\Desktop\Outx"
GOTO :ScriptB
:ScriptB
::REM ONLY CHANGE
SET "sourcedir=%USERPROFILE%\Desktop\Test"
FOR /R "%sourcedir%\" %%a in (*.txt) do copy "%%a" "%CD%\Storage"
:ScriptC
:ScriptC
#ECHO OFF
SETLOCAL
SET "VARA=%CD%\Storage"
SET "VARB=%USERPROFILE%\Desktop\Out"
::REM ONLY CHANGE
SET "mystring=PUT_STRING_HERE"
FOR %%a IN ("%VARA%\*.txt") DO FINDSTR "%mystring%" "%%a">nul&IF NOT ERRORLEVEL 1 FINDSTR "%mystring%" "%%a">"%VARB%\%%~nxa"
DEL /F "%CD%\Storage\*.txt"
GOTO :ScriptD
:ScriptD
#ECHO ON
COPY /B "%USERPROFILE%\Desktop\Outx\*.txt" "%CD%\Storage\export.txt"
RD /S /Q "%USERPROFILE%\Desktop\Outx"
goto :eof

Copy files from folder of lnk files back into folder

I have a bunch of folders, each containing a number of shortcut link files to mp3 files existing in completely separate folders. eg:
/rock-mp3-shortcuts
/jazz-mp3-shortcuts
/funk-mp3-shortcuts
what command would I run (or program to use) to copy all the underlying mp3 files back into the folders of shortcuts that are pointing to them.
I basically want to get all the files in each genre folder of shortcuts to then copy into my portable mp3 player.
This should work:
#echo off
FOR /r %%i in (*.lnk) do call :COPYFILE "%%i"
GOTO:EOF
:COPYFILE
set "filename=%1"
set "filename=%filename:"=%"
set "filename=%filename:\=\\%"
for /f "tokens=1* delims==" %%I in ('wmic path win32_shortcutfile where "name='%filename%'" get target /format:list ^| find "="') do (
set tatgetFile=%%J
copy /y "%tatgetFile%"
)
You'll have to create a bat file and paste my code into it. The file must be located in the folder where all your *.lnk (shortcut) files are. As you have three of them, you will have to copy the bat to each folder and execute it once. You also can automate this and use only one bat but I guess you'll figure out yourself how to do this. It will iterate over all shortcuts and copy the target files to the current folder.
Unfortunately, handling shortcuts in cmd is a pain in the 'a'. That's why we have to use wmic and win32_shortcutfile here.
You can check shortcutJS.bat with which you can create or check info about .lnk.You will need it in the same directory with this code:
#echo off
setlocal
::set your location on the line bellow
set "mp3_dir=c:\mp3_dir"
pushd "%mp3_dir%"
for /r %%# in (*.lnk) do (
for /f "tokens=1* delims=: " %%a in ('shortcutJS.bat -examine "%%~f#"^|find /i "target"') do (
echo location of %%# : %%~fb
rem !!!! remove the echo on the line bellow if everything is ok !!!!
echo copy "%%~fb" "%%~dp#"
)
)
endlocal

Resources