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.
Related
I wish to copy a file with extension .dyn which is located in each subfolder of main folder(T15_finished). I wish to copy it into respective subfolder at other location(T15). I have created that location using xcopy command. Here, .dyn file is being copied successfully in respective subfolder in T15 folder(see below code). Now, I have a file which has extension as .dynain which is located in the same subfolder as .dyn. And .dynain file is also getting copied which i don't want.
Please see following code which i have created. Can anyone tell me whats wrong ?
#echo off
xcopy D:\Master\SPRINGBACK\CPW\T15_finished D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15 /t
xcopy /e D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15
pause
Short file names. If you do a dir /x in the folder containing the .dynain file, you will see the 8.3 filename generated for the file, and it will have .dyn extension.
If you know the extensions of the conflicting files, you can use robocopy with /xf switch to indicate the files (*.dynain) to exclude, or you can generate a exclude file to use with xcopy /exclude:file (see xcopy /? for a explanation)
Or, you can generate the list of files to exclude
(for /f "tokens=" %%a in (
'dir /s /b "D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn" ^| findstr /v /i /e /l ".dyn"'
) do #echo(%%~nxa)>excludedFiles.txt
xcopy /exclude:excludedFiles.txt /e D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15
Or (as posted by foxidrive), copy all and then delete the non needed files.
The short filename is being matched as well as the long filename. That is the reason.
A solution is to use another command to delete the files:
del /s "D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15\*.dynain"
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 folder and paste it in the same directory it was copied from.
For example
C:\Test is the main directory which consists of a folder ACDM, I would like to copy ACDM in the same directory and rename the new folder to ACDM1 which will have all the same files as ACDM has
I would like to do it using command prompt
I tried the following
C:>Xcopy C:\Test C:\Test\ACDM1 /E /U
Cannot perform a cyclic copy
0 File(s) copied
which fails, not sure hoe to add REN command with XCOPY command.
Need help ASAP as i would want to create a batch file which will create a copy of an existing folder and rename it according to a name retrieved from a text file..
xcopy "C:\Test\ACDM\*.*" "C:\Test\ACDM1\" /s/h/e/k/f/c
for /f "delims=" %%a in (yourtextfilename) do xcopy "C:\Test\ACDM" "C:\Test\%%a\" /E
as a .bat file line. Directly from the prompt, change each %% to %
I've assumed (for lack of futher information) that your textfile contains just the one line
ACDM1
neither do you specify the textfilename tou want to use.
xcopy C:\Test\ACDM C:\Test\ACDM1\ /E /Q
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 am very new to batch scripting.
I need to write a batch script to search for a specific file(usually .zip or .7z extension) located on network drive directory(containing multiple folder and sub-folders with space in name) and copy the same to my local drive.
Also I need to copy a zip file containing "elf" keyword which will also be located in the same directory where my file is present.
For example:
Search file: abc.zip
Network drive:\abc.com\test
So basically I need to search for my file abc.zip in the network directory(including sub-folders) and if found copy the same to my local drive(say c:\Temp) and also I need to copy *elf* file to the same local directory.
Thanks in Advance.
#echo off
rem Prepare environment
setlocal enableextensions
rem Configure paths
set "source=\abc.com\test"
set "target=c:\test"
rem Change drive/path to source of files
pushd "%source%"
rem Recurse folders searching adecuated files and when found, copy to target
for /f "tokens=*" %%f in ('dir /s /b "abc.*z*" ^| findstr /i /e /c:".zip" /c:".7z"') do (
copy /y "%%~ff" "%target%"
if exist "%%~dpf\*elf*.zip" copy /y "%%~dpf\*elf*.zip" "%target%"
)
rem Return to previous drive/path
popd
rem Clean
endlocal
This will search the source folder hierarchy for indicated filename and an aproximate match based on file extension (.*z* matchs .zip, .7z and more), filters file extensions to only accept the cases needed (.zip and .7z) and copy the required files to target folder.
Have you considered using the Extended Copy Utility (xcopy)? Syntax is as simple as
xcopy "<directoryToCopyFrom>" "<directoryToCopyTo>" /C /S /D /Y /I
This will work assuming you're wanting to write a Windows batch script.
Searching for the "elf" string will be a bit trickier though. You might consider doing that using Java, and then call the batch file from the Java program.
for /d /r "drive:\abc.com\test" %%A in (*) do (
if exist "%%~A\abc.zip" copy "%%~A\abc.zip" "C:\Temp"
if exist "%%~A\*elf*" copy "%%~A\*elf*" "C:\Temp"
)