I'm trying to automate a little tedious process I have to go through while updating files on my server. I have a content folder, with a lot of subfolders, each potentially containing some files. Some files have a compressed version (ending with .bz2). So a folder could have something like:
sound1.wav
sound1.wav.bz2
sound2.wav
texture1.tex
texture2.tex
texture2.tex.bz2
What I want to do is remove every file (somewhere in the content folder) that has an equivalent compressed file. Meaning in the above example I just want 'texture2.tex' and 'sound1.wav' removed.
for /r %%f in (*) do if exist "%%f.bz2" del "%%f"
Or, at the command line instead of in a batch file:
for /r %f in (*) do if exist "%f.bz2" del "%f"
Little mistake. It should be:
for /r %%f in (*.bz2) do if exist "%%f" del "%%f"
Or, at the command line instead of in a batch file:
for /r %f in (*.bz2) do if exist "%f" del "%f"
Related
I am trying to find specific files within a folder and its sub-folders and copy them to a new folder. I used to use this batch file for this purpose but now I get this error :
The system cannot find the file specified.
Here's the batch-file content:
pushd "\\internal.company.com\path\"
md myfile
FOR /R "\\internal.company.com\path\" %%G in (prefix_myfile*) do copy %%G "\\internal.company.com\path\myfile"
Any input is appreciated.
UPDATE
I tried printing %%G like this:
FOR /R "\\internal.company.com\path\" %%G in (prefix_myfile*) do echo %%G
and it works well. The problem arises with copy command which cannot read %%G as an argument.
I'd change to
... copy "%%G" ...
which would cater for spaces in %%G.
Perhaps you should determine whether the problem is with the for/r or %%G. If you simply display %%G with an echo %%G command, then you may see some problem. If it still won't run, then the for /r is the problem.
Since you are pushding to the appropriate directory, there is no need to specify the target directory in the for/r. It can be omitted or replaced by .
or perhaps echo %cd% directly after the pushd to show whether the pushd is the cause of the problem.
I realized that this would work for me (executed in cmd). However, it is plausible because I don't have any *.txt file in the main directory. Not the best solution but a workaround.
pushd "\\internal.company.com\path\"
rem first copy the desired files (text files) to the main-folder
for /f "tokens=*" %f in ('dir /a:-D /s /b myfile*') do copy "%f"
rem then make a new-folder and move them to there
md myfile
move *.txt "\\internal.company.com\path\myfile"
Note: if you want to execute this as a batch file, you need to use %%f instead of %f.
This will copy the files into main folder and then moves them into the desired sub-folder.
I am new to programming, thanks to the new task my boss has provided.
I am trying to run a batch file to zip multiple files in a folder separately.
So, I want file1.txt to zip to file1.zip and so on for other files.
I have only the following code:
for /f "tokens=*" %f in ('dir /b *.DAT') do "c:\Program Files\7-Zip\7z.exe" a "%f.7z" "%f"
My issue: When I run it on cmd after navigating to the target folder, it works, but when I store it in a batch file and run it from the target folder, it wont work.
Please help me identify what and where I need to make changes in my code.
Regards
AK
WIthin a batch file, the metavariable (loop-control variable) requires the % to be doubled, so replace each %f with %%f when you mean the loop-control variable"f"`
Your code seems fine. The issue is, that when you run it inside a batch file, you need to put an extra % mark.
In the batch try:
for /f "tokens=*" %%f in ('dir /b *.DAT') do "c:\Program Files\7-Zip\7z.exe" a "%%f.7z" "%%f"
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'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\"
)
I'm trying to copy the contents of a directory using a DOS batch file that begins with the computer name followed by an underscore and a date stamp. My first impulse was some variation of:
copy D:\%Computername%_\*\\*.* C:\WhateverPath
Of course I could not get this to work. Seems like a simple problem but I don't have much experience with batch files or DOS.
Try:
FOR /d %d IN (D:\%COMPUTERNAME%_*) DO xcopy %d C:\WhateverPath /E
This iterates over all directories (hence the /d) with the pattern %COMPUTERNAME%_* under D:\, and copies the contents of these directories into C:\WhateverPath. /Eis for copying all files and directories, also the empty ones.
For documentation of xcopy, type xcopy /? in a DOS shell (cmd).
Note: If you put this in a batch-file (something.bat), you must replace %d with %%d in the code above.
If you have multiple folders labeled C:\%computername%_%random_time_stamp%\ and you need to access each of them then move all of their contents to a single folder, you can do this:
Given the only underscore in the path is the one between %computername% and your timestamp
FOR /F "USEBACKQ tokens=*" %%F IN (`DIR /b /a:d "C:\" ^| FIND /I "%computername%_"`) DO (
COPY /y "%%~fF\*" "C:\WhateverPath\"
)
That states for every result that comes from the command DIR, /b switch meaning no header information, /a:d meaning only returning directories, I want to find only folders with the computername_ in it, and I want to copy the contents of each of those folders to C:\WhateverPath\ folder.