I've been struggling with this one for a couple days now, as I'm not very code-savvy. Basically, I was given a giant zip file full of reports that someone needs access to. When that's extracted, it has a full directory structure, and all the files are .tar.gz files. I've got a working batch file to extract all .tar.gz to .tar, and then delete the .gz's.
The problem I'm facing now is that inside the TAR's, all the file names are meaningless as a series of numbers. I'm trying to make a .bat that will extract the TAR's and rename the contents to the same file name as the .tar had. (There is only one file per TAR).
Here's what I've got at the moment:
FOR /D /r %%F in ("*") DO (
pushd %CD%
cd %%F
SET
FOR %%X in (*.tar) DO (
set filename="%%X"
"C:\Program Files\7-zip\7z.exe" x "%%X" -aou -o"%%F\temp\"
REN "%%F\temp\*" %filename%
REN "%%F\temp\*.tar" "*.null"
MOVE "%%F\temp\*.null" "%%F"
RMDIR "%%F\Temp\"
DEL "%%X"
)
popd
)
I've tried loading %%F into variables, but that caused another set of problems. I have a final script that uses TrID to identify and correct file types, so the .null value is just temporary.
Test this - it is meant to extract the TAR file, rename the extracted file to the same name as the TAR with .null on the end, and then delete the original TAR file and move the renamed file to where the original TAR file was, removing the temp folder.
It's meant to be run in the main folder with all the TAR files in folders beneath it. Copy some to another folder and try it.
#echo off
FOR /r %%F in (*.tar) DO (
"C:\Program Files\7-zip\7z.exe" x "%%F" -aou -o"%%~dpF\temp\"
REN "%%~dpF\temp\*.*" "%%~nF.null"
DEL "%%F"
MOVE "%%~dpF\temp\%%~nF.null" "%%~dpF"
RMDIR "%%~dpF\temp\"
)
Related
when it comes to creating a batch-file I would sadly call myself a newbie and therefore it's kind of difficult for me to achieve what I want on my own:
so here is how my codes look:
#ECHO OFF
for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
del C:\ReadyToExport\*.pdf*
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
PAUSE
and here is what the code exactly does
1- for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
this line copies all files from source to another target folder the good thing is that this command copies all the files inside other subfolders from source and paste them into the folder C:\ReadyToExport without any subfolders.
2- del C:\ReadyToExport\*.pdf*
this command filters all the .pdf files because they are unnecessary
3-
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
this command basically copies all files from "C:\ReadyToExport" into C:\import and writes the name of the recorded file in LogFile.log then the copied files will be excluded when the script runs again because I don't want to copy the files again if I already copied them before
and here is what I want to achieve:
I want to copy only modified files from the folder "C:\ReadyToExport" to the target C:\import but keep in mind that
the files in the target folder "C:\import" will be deleted but since the names of the files that have already been copied are registered in LogFile.log the files will be excluded and not copied again.
in another word: files in target file do not exist anymore but their names are written in LogFile
so is there any way to copy only modified ones? even though they don't exist in the target folder anymore? but their names are in LogFile.txt? can the script somehow write the last modified date in LogFile.txt near the file name? and then compare the date from the source? so it copies only files that have been changed and ignore all files that didn't?
p.s: using Xcopy, robocopy, etc is not a problem
any answer will be appreciated.
thnx
your method does, in fact, works so thank you so much not the way the I want with exclude:logfile.log
but this definitely worked:
robocopy C:\ReadyToExport\ C:\import /M /E
since it copies only the files that hast the attribute archivable checked in the settings.
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
if EXIST C:\Users\g511411\Desktop\unsigned\*.tar (for /R C:\Users\g511411\Desktop\unsigned\GNU_ARM_DEBUG %%f in (*.bin) do copy "%%f" C:\Users\g511411\Desktop\dll\Destination
for /R C:\Users\g511411\Desktop\unsigned\GNU_ARM_DEBUG %%f in (*.txt) do copy "%%f" C:\Users\g511411\Desktop\dll\Prm)
I have an unsigned folder which has .tar file and GNU_ARM_DEBUG folder. According to second command in if: if .tar is present in unsigned folder then copy .txt from GNU_ARM_DEBUG folder to Prm folder. But GNU_ARM_DEBUG also has Resources folder and this command also copies .txt file from Resources folder which I don't want. What should I do?
I'd replace your second line with
xcopy "C:\Users\g511411\Desktop\unsigned\GNU_ARM_DEBUG\*.txt" "C:\Users\g511411\Desktop\dll\Prm\"
)
Note that adding /L to the xcopy command will simply display a List of the copies batch proposes to do - good for checking that the command is correct.
You could also add /y to the xcopy to auto-overwrite existing files, and add >nul / 2>nul to the end of the line to suppress reports/error reports.
I am trying to copy all files from several folders to one folder in the same directory.
I have create a Batch file, which contains
MD PATCHCON
for /R %cd% %%f in (*.*) do copy %%f %cd%\PATCHCON
pause
If I put this on the desktop, it runs successfully; if I run the same code in the dir it's not working.
This code also copies my batch file in consolidate folder<patchcon> so I also want to add a code line that does not copy my batch file.
You have a couple of issues which you may not have realised.
Firstly it will fail in folders or files that have a space or & in the name or path.
The other issue is that it will try to copy some files in the PATCHCON folder twice.
This should solve those problems and remove the batch file itself from the folder.
#echo off
MD "..\PATCHCON"
for /R "%cd%" %%f in (*) do copy "%%f" "..\PATCHCON" >nul
del "..\PATCHCON" "%~nx0"
move "..\PATCHCON" . >nul
echo done.
pause
I am trying to copy a set of files and folders recursively from a directory. The set of files are listed in a text file. Here is my script:
set src_folder=C:\Users\mmhuqx\HW
set dst_folder=C:\Unix2Windows
set filelist=C:\Unix2Windows\filelist-tm.txt
echo Origen: %src_folder%
echo Destino: %dst_folder%
echo.
for /f "delims=" %%i in (%filelist%) do (
xcopy /s /y "%src_folder%\%%i" "%dst_folder%"
)
But When I run the script it copies the entire Directory contents including the files and folders not listed in the text file.
How can I make it work using XCOPY, or is it not possible?
Your filelist-tm.txt seems to be wrong. It contains for example an entry TOP_LEVEL\MODEM_DEBUG. So that directory is copied completely with all its files.
It seems that you only want TOP_LEVEL\MODEM_DEBUG\AENEAS_FW.fls to be copied. So your filelist-tm.txt should only contain this entry.