I have about 120,000 zip files that I am using the following command to extract:
FOR /F "usebackq" %a in (`DIR /s /b *.zip`) do 7z.exe e %a
It extracts the contents of the files just fine but I created the zip files with a naming system and was wondering how to be able to apply the name of the zip file to the files within each zip.
For example
DR80005.zip contains Moo_Cow_Serenade.pdf
I'd like it to extract as DR80005.pdf
Is there any way to do this?
I can think of a batch-based solution:
Do the same loop you have but redirect the (7z) extraction to another folder/directory
"extract" the name of the zip file to a variable
list the contents of what is in the extraction directory (the last extracted file) and rename/move it (with the name of the zip file you have stored in 2. and the same extension) to the final folder/directory
Just make sure the extraction folder/directory is empty to start with.
EDIT: Here is the code (already "optimized", from the above pseudo-code), for completion:
FOR /F "usebackq" %%a in (`DIR /s /b *.7z`) do (
7za.exe e %%a -otempfolder
ren tempfolder\*.* %%~na.*
move tempfolder\*.* finalfolder
)
Notes: tempfolder is the temporary folder (needs to exist and be empty, prior to execution). In much the same way finalfolder is where all the extracted files will end up, with the new name.
Plus. This code assumes the zip files only have one compressed file inside.
Also. 7za.exe is the command-line executable (of 7zip) .
Related
I need to delete specific files from 28 folders on the same server.
e.g
C:/folder/DMP/app_x0
C:/folder/DMP/app_x1
C:/folder/DMP/app_x2
DeleteList.txt has a list of files names (with path).
C:/folder/DMP/app_x0/ABC1.txt
C:/folder/DMP/app_x0/ABC1.doc
The batch file needs to have a loop to go through each folder one by one and delete all files mentioned in a text file. Following worked ok for one folder only if I specify the full path before each file's names in DeleteList.txt file.
for /f "delims=" %%f in (DeleteList.txt) do del "%%f"
How to use above so that same code could run 28 times in batch file but each time replaces folder location path. DeleteList.txt will not change.
Any sample code/suggestion would help.
Thx.
One of these questions where we need to read between the lines.
Given we have on file of full filenames with the incorrect path separator, and a requirement to delete the files from 28 directories (but there's no list) then I conclude that since the required subdirectories shown are all subdirectories of C:\folder\DMP\ then the requirement actually is to delete all of the files that match the filenames in the file in all subdirectories of the parent directory of the full filename in the list.
So my suggestion would be
for /f "delims=" %%b in (DeleteList.txt) do del /s "%%~dpb..\%%~nxb"
Naturally, you should test this on a dummy tree before implementing it.
del /s deletes in the target directory and all subdirectories. The target subdirectory is the drive and path of %%b; its parent (..); the name and extension part of %%b.
I prefer to use metavariables that are not available as metavariable-modifiers.
To delete in subdirectories but not in the parent:
for /f "delims=" %%b in (DeleteList.txt) do FOR /d %%c IN ("%%~dpb..\*") DO del /s "%%c\%%~nxb">nul
This time, %%c is set to all subdirectorynames in turn, and the del/s applied to each subdirectory. The /s (...and subdirectories) probably isn't required. The >nul suppresses the deletion report if desired.
Not sure if the OP has need for this anymore, but you can use the following batch script to delete the specific files from multiple folders, starting from a parent folder.
#echo off
for /r "Drive:\path\to\parent_folder" %%d in (.) do (
del "%%~d\ABC1.txt"
del "%%~d\ABC1.doc"
)
The script will loop through the parent directory and its sub-folders, and delete only the specific files from the folders that they appear.
In the case of the post, the parent folder can be "C:\folder".
So, this is a particularly puzzling problem for me. I'm open to using whatever I must, but I typically only write simple batch files.
I have a huge research project at the hospital where I work, and the data I'm working with is presented to me with a 4 digit subject identifier followed by a timestamp, as seen in this example:
6443_20170419_141200416
6443_20170419_141200447
6443_20170419_141200500
6476_20170419_141200537
6476_20170419_141201112
etc.
I have literally thousands of these folders, and in each of them is between 1 and 3 files with very long file names - the only commonality is the .DCM extension.
What I'd like to do is have a script that will extract the first 4 characters of the folder name, create a new directory with that 4 character name, and then copy any files located within folders with the matching prefix into the newly created folder.
For example, let's say the folders which all start with 6443 have several .DCM files in them. I want to create a new folder named 6443 (in a different location that the then current directory, to avoid accidental deletion), and then move all of the .DCM files from each directory into the new folder.
I have a .TXT file which contains all of the subject ID #'s that I've been using for various other scripted tasks, using FOR /F to walk this file, if that gives anyone an idea for a solution.
This once is really picklin' my noggin. Help!!!
----- ADDITIONAL INFO -----
I've been making progress, but it's still not right. I'm using the script as shown below, but it's moving ALL of the files in each of the folders to each of the newly created folders, instead of sorting them by 4 digit prefix.
#ECHO on
cls
FOR /f "delims=_" %%a IN ('dir /b /ad "*_*_*"') DO (
if not exist %%a MD .\combined\%%a
FOR /d /r %%d IN ("*") DO (
copy %%d\* .\combined\%%a\*
)
)
Here is the corrected script:
#echo on
cls
for /F "tokens=1* delims=_" %%a in ('dir /B /A:D "????_*"') do (
if not exist ".\combined\%%a\" md ".\combined\%%a"
copy ".\%%a_%%b\*.dcm" ".\combined\%%a"
)
What I changed:
the tokens option has been added to the outer for /F loop in order to be able to rebuild the original source directory name later;
the directory search pattern has been improved;
the if query checked the wrong directory;
the inner for loop has been removed, because it iterated over all source directories again;
the source and destination paths for the copy command have been adapted;
all paths are properly quoted;
I'm attempting to sort a lot of files based on the current location of the file e.g.:
File 1 is located at C:\Work\Movies\Subs\Subtitle.txt
File 2 is located at C:\Work\Movies\Subs\Special\Subtitle.txt
File 3 is located at C:\Work\MoviesSpanish\Subs\Subtitle.txt
I'm trying to move the files like so:
File 1 to C:\Work\InProgress\Movies\Subs\Subtitle.txt
File 2 to C:\Work\InProgress\Movies\Subs\Special\Subtitle.txt
File 3 to C:\Work\InProgress\MoviesSpanish\Subs\Subtitle.txt
The Batch Script is to be located in C:\Work\MoveFile.bat
There are away more files then I listed above. Just for an estimate I would say around 300-500 per folder and there's a lot more subdirectories (e.g. .\Subs\01\ all the way up to .\Subs\300\ and they each contain a bunch of text files). I need to move all of the .txt files from their current locations to a new folder in C:\Work\ while retaining the rest of the directory location. So they get moved to C:\Work\[New Folder]\[Rest of Original Location]
I want to do this in batch but I'm not sure where to start. I already have the following code, which deletes files that don't contain a specific string:
for /r %%Z in (*.txt) do (
SET "count="
for /F "usebackq delims=," %%A in ("%%Z") do (
if /i "%%A"=="LN" set count=1
)
if not defined count echo del "%%Z"
if not defined count del "%%Z"
if defined count move "%%Z"
echo %count%
echo %%Z
)
But I'm not sure how to obtain the correct directory to move them into. I was thinking about for loop that reads the directory string of the file in question and uses delims=/ but it kept reading the text file rather then the path (and when I didn't use quotes on %%Z, it decided it couldn't find the file).
This is untested - it uses robocopy to move the *.txt files into a new location which is outside the source folder (because of cyclic copy restrictions) and then moves them back to the new "C:\Work\InProgress" folder.
If %temp% and c:\work are on the same drive then it should be swift.
Change c:\work in both places to a test folder with a few copies of your files and test it.
#echo off
robocopy "C:\Work" "%temp%\temporarymoviefolder" *.txt /s /mov
robocopy "%temp%\temporarymoviefolder" "C:\Work\InProgress" *.txt /s /mov
pause
I have a folder structure like this:
C:\\\Logs\logs1\tracelogXXXX.log
C:\\\Logs\logs2\tracelogXXXX.log
C:\\\Logs\logs3\tracelogXXXX.log
Each folder has a bunch of tracelogXXXX's, and I've got the pseudocode for a script that loops through each folder, archives each log into its own .zip, and then delete the tracelog left outside the archive (because 7zip doesnt have move functionality).
But I have no batch experience really, and I can't even get the zipping to work properly.
I can't access the documentation for 7zip from where I am currently, so I've tried this:
CD C:\Logs
FOR /R %%i IN ("*.log") DO "C:\...\7za.exe" a -tzip "%%i.zip"
And also this:
CD C:\Logs
FOR /R %%i IN ("*.log") DO "C:\...\7za.exe" a -tzip "%%i.zip" "%%i\"
The first one goes and zips all of \Logs for each instance of a .log file, making many zips each bigger than the last. And the second makes zips for each instanceof a .log file, with nothing in them.
How do I just zip each log file, in its own zip, named after itself, while operating from the parent directory? Deleting the outer files afterwards doesn't seem hard to accomplish once I figure out whats wrong with this syntax, but this is the important part!
You can do this from the command line with no batch file needed:
FOR /F "usebackq tokens=* delims=" %A IN (`DIR "C:\Logs\*.log" /B /S`) DO "C:\Path\To\7za.exe" a "%~dpnA.zip" "%~fA" & DEL "%~fA"
To use in a batch file, just replace each % with %%.
Can anyone assist me in writing a batch script that searches for file names and outputs the location of the files to a text file. Example I have a file called list.txt located in a folder, C:\LocateFiles\list.txt. Located in the list.txt file are about 25 file names that I wish to determine if they are anywhere on the C:\ drive. If it locates any of the file names identified in the file list.txt it will output the path of all files found to a single file in C:\LocatedFiles\results.txt.
A million thanks,
Johnny Mac
#ECHO OFF
FOR /F %%F IN (C:\LocateFiles\List.txt) DO DIR /s/p/b %%F.* >> C:\LocateFiles\finds.txt
Save that as LocateFiles.cmd and place it in whichever directory you wish to search, note that C:\ is very large and will take quite a while! as in, forever, seriously, i really wouldnt, your call...
the file finds.txt will have the entire path for any file that matches up to the file names listed in List.txt
Also note, this finds files of any extension, but the filename itself must match exactly to whats in List.txt
The solution below search the files in the current directory just once, so it run faster.
#echo off
dir /S /B /A-D | findstr /I /G:C:\LocateFiles\list.txt > C:\LocatedFiles\results.txt
EDIT: New method added
The method below may run even faster. It is necessary to complete a timing test.
#echo off
setlocal EnableDelayedExpansion
rem Read file names from file list and assemble a long string with this format:
rem "filename1.ext*" "filename2.ext*" ...
set "fileList="
for /F "delims=" %%a in (C:\LocateFiles\list.txt) do set fileList=!fileList! "%%a*"
rem Search the files from current directory downwards
(for /R %%a in (%fileList%) do echo %%a) > C:\LocatedFiles\results.txt