I'm trying to copy some non specific folders with their content, but can't find a way to do it. My problem is every time I try to use any of the copy commands it tells me it's an "Invalid number of parameters", or "0 File(s) copied", or "File not found -", or it copies everything from Folder1, (not just the non specific folders).
What I mean by "non specific" is a folder which has an unknown name, i.e. *.
This is what I have tried:
xcopy /y "C:\Folder1\Something_*" "C:\Folder2"
copy /y "C:\Folder1\Something_*" "C:\Folder2"
robocopy "C:\Folder1\Something_*" "C:\Folder2"
/+ adding " * " in front of "Something_*"
I have also tried a lot more smaller things, that I don't see a need to add.
I'm a little lost on what to do at this point, and feel like I have been looking everywhere for a solution with no luck.
Edit: i found a way to do this and it's so much better, this is how
for /R "%sourcedir%" %%A in (Something_*) do copy /y "C:\Folder1\%%~A" "C:\Folder2"
#ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
SET "destdir=u:\your results"
:: Version 1 - all files arrive in the destination directory
FOR /f "delims=" %%b IN ('dir /b /ad "%sourcedir%\Something_*" ') DO (
COPY /b /y "%sourcedir%\%%b\*" "%destdir%\" >NUL 2>nul
FOR /f "delims=" %%c IN ('dir /b /s /ad "%sourcedir%\%%b" ') DO (
COPY /b /y "%%c\*" "%destdir%\" >NUL 2>nul
)
)
cls
dir/s "%destdir%"
PAUSE
:: This is a routine I use for testing. It simply deletes the directory named and restores it, empty
CALL deltreey /r "%destdir%">NUL
:: Version 2 - preserve tree structure
FOR /f "delims=" %%b IN ('dir /b /ad "%sourcedir%\Something_*" ') DO XCOPY /y /s /e "%sourcedir%\%%b" "%destdir%\" >NUL 2>nul
)
cls
dir/s "%destdir%"
PAUSE
:: This is a routine I use for testing. It simply deletes the directory named and restores it, empty
CALL deltreey /r "%destdir%">NUL
GOTO :EOF
Related
I'm trying to pipe a string that contains folder paths to findstr to search for a particular part in the names of the given folders - or at least, that is what I'm planning to do.
I've got a source folder with files that have to be copied into multiple subfolders and after that, one of the copied files has to be renamed corresponding to the destination folder. If there already are files with the same names, they have to be overwritten. I am trying to achieve this via .bat-file using the following commands in my code:
pushd ..\..\destination_folder\
FOR /F "delims=" %%i in ('dir /AD /S /B^| findstr /I "._Modul_X$"') do copy ..\xxx\yyy\ressources\*.* %%i
& ren %%i\xxxx_Modul_X.BAT_TEMPLATE" "%%i_Modul_X.BAT_TEMPLATE
The copy-part seems to work, the rename-part does not and when it comes to overwriting the one file that has to be renamed after copying it (name conflict!), I'm pretty clueless how to do this (IF EXIST & DEL?).
If I understand the process you're attempting, then the following should do as you require, subject to my assumption that xxxx is a sequence of exactly four, digits (directory names), and characters (file names):
#Echo Off
SetLocal EnableExtensions
PushD "..\..\destination_folder" 2>NUL || GoTo :EOF
If Not Exist "..\xxx\yyy\resources\*.*" GoTo :EOF
For /F "Delims=" %%G In (
'Dir /B /S /A:D "????_Modul_X" 2^>NUL ^|%__AppDir__%findstr.exe^
/I /R "\\[0123456789][0123456789][0123456789][0123456789]_Modul_X"'
) Do (
For /F "Delims=" %%H In (
'%__AppDir__%where.exe /F "%%G":"????_Modul_X.BAT_TEMPLATE" 2^>NUL'
) Do Del /A /F %%H
Copy /Y "..\xxx\yyy\resources\*.*" "%%G" 1>NUL
For /F "Delims=" %%I In (
'%__AppDir__%where.exe /F "%%G":"????_Modul_X.BAT_TEMPLATE" 2^>NUL'
) Do If /I Not "%%~nxI" == "%%~nxG%%~xI" Ren %%I "%%~nxG%%~xI"
)
Please note that you will probably need to modify both instances of xxx\yyy\resources, (lines 5 and 14), as nobody really uses names like that, paying special attention to the spelling, I've used resources above, not ressources.
I need to copy source file to destination folder using bat file.
I have created this:
#echo off
set source="D:\Folder1\file.dll"
set destination="D:\Test\TestCopy*\Test\"
xcopy /s /y %source% %destination%
pause
The destination path I have is TestCopy.2.5.3.6. This numbers can change. So specifying TestCopy* is not working. Specifying TestCopy.*.*.*.* also not working.
How can I solve this?
That is not the way xcopy is used; it cannot copy directory structures/files to multiple folders, e.g. copy file random.ext to C:\folder1\test, C:\folder2\test, etc.
Also, no need to use xcopy to copy files. Just use copy instead.
To achieve this, use:
#echo off
pushd "D:\Test"
set source="D:\Folder1\file.dll"
for /F "delims= eol=" %%A IN ('dir /B /AD "D:\Test\TestCopy*"') do (
copy %source% "%%~fA\Test\"
)
popd
Or, better one-line for /F:
#echo off
pushd "D:\Test"
set source="D:\Folder1\file.dll"
for /F "delims= eol=" %%A IN ('dir /B /AD "D:\Test\TestCopy*"') do copy %source% "%%~fA\Test\"
popd
This is for the situation when you have multiple TestCopy* subfolders:
::It's better to comment out #echo off when you are testing the batch file
::#echo off
::Moved the open quote's position, so the source variable won't have quotes inside.
set "source=D:\Folder1\file.dll"
for /F "delims=" %%i IN ('dir /ad /b "D:\Test\TestCopy*"') do call :fcopy "%%i"
::Comment or remove pause when it's okay.
pause
goto :eof
:fcopy
if not exist "D:\Test\%1\Test\" goto :eof
xcopy "%source%" "D:\Test\%1\Test\"
Used a sub procedure :fcopy, pass each TestCopy* subfolder name to it.
All :: starting lines are comment lines. They are not executed.
I think it's a good habit to quote every path variable when used, but don't include the quotes themselves in the variable -- that's why I moved the quote in the set source line.
I need to search a directory with multiple folders and check for the latest file(.exe) and copy that to another location.
SET "src_root"
SET "tgt_path"
DIR "%src_root%" /B /AD /O-D /TC > "%TEMP%\dirlist.tmp"
< "%TEMP%\dirlist.tmp" SET /P last_dir=
XCOPY "%src_root%\%last_dir%\*.exe" "%tgt_path%"**
This code helps me copy the EXE file in the latest folder, but in case, there is no EXE in the latest folder, I need to copy it from the folder which contains the latest EXE, can anyone help me out?
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "filename="
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\neo.7z" '
) DO SET "filename=%%a"&set "dirname=%%~dpa"&goto found1
ECHO NOT found!
GOTO :eof
:found1
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\neo.7z" '
) DO IF /i "%dirname%" neq "%%~dpa" FOR /f %%s IN ('XCOPY /y /L /D "%filename%" "%%~dpa"') DO IF "%%s"=="0" SET "filename=%%a"&set "dirname=%%~dpa"&goto found1
ECHO latest file is "%filename%"
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used files named neo.7z for my testing - I believe that you should have a fixed filename in the situation you describe - the directories are no doubt not crammed with *.exe files.
It's very pedestrian, but will do the job.
Essentially, find any file with the required name in the required directory-tree and record the name in filename, directory in dirname.
Using that filename as a base, try xcopying it over every other matching filename. Use the /L flag to list-only, and the /y flag to remove user-intervention. If the return is 1 file(s) copied ,f which only the first token is selected into %%s, then the chosen file is later. 0 file(s) copied sets %%s to 0 and means that the chosen file is earlier, so select the newer file and directorynames and restart.
I'll leave it a a user-exercise to speed it up. (suggestions if interested : delayedexpansion, subroutine and retain-new-startpoint)
Revision [about 5 times faster]
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "filename="
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\neo.7z" '
) DO SET "candidate=%%a"&CALL :latest
ECHO latest file is "%filename%"
GOTO :eof
:latest
IF NOT DEFINED filename GOTO selectnew
FOR /f %%s IN ('XCOPY /y /L /D "%filename%" "%candidate%"') DO IF "%%s"=="1" goto :eof
:selectnew
SET "filename=%candidate%"
GOTO :EOF
This revision avoids the repetitive directory-scan. For each matching filename, compare the newly-found file against the previously-found file using the xcopy /L method, and select the new candidate if the new one is later. continue untill all matching names have been tested.
I want to write a batch file that creates a folder (if it does not exist) and copies a certain file into that folder. So far so good.
The problem is that one folder in the path varies slightly from time to time, so a wildcard becomes necessary.
The following code works just fine but obviously misses to create the folder (Reports). So if the folder is not there, it simply does nothing.
for /r "c:\Users\%USERNAME%\AppData\Local\Packages" &&G in ("LocalState\acn\Reports") do #if exist %%G xcopy /s /i /y c:\temp\Reporting "%%G"
The full path is:
c:\Users\FSchneider\AppData\Local\Packages\“WILDCARD"\LocalState\acn\Reports\
Any idea?
Add /d switch in for to indicate you're looking for a directory, not a file
Add * and omit quotes in the wildcard to indicate it's actually a wildcard
No need for if exist now
for /d /r "%LocalAppData%\Packages" %%G in (LocalState\acn.*) do xcopy /s /i /y c:\temp\Reporting "%%G\Reports"
Next script could help.
#ECHO OFF
SETLOCAL enableextensions
set "_fldrtop=%USERPROFILE%\AppData\Local\Packages"
set "_fldrsub=LocalState\acn"
if not "%~1"=="" set "_fldrsub=%~1" :: my testing data, remove this line
set "_fldrlow=Reports"
if not "%~2"=="" set "_fldrlow=%~2" :: my testing data, remove this line
for /F "delims=" %%G in ('dir /B /AD "%_fldrtop%"') do (
if exist "%_fldrtop%\%%G\%_fldrsub%\" (
if exist "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\" (
echo echo "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
) else (
echo md "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
)
rem echo xcopy /s /i /y c:\temp\Reporting "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
)
)
Output:
==>D:\bat\SO\31672436.bat
==>D:\bat\SO\31672436.bat "LocalState\Cache"
md "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\Reports\"
==>D:\bat\SO\31672436.bat "LocalState\Cache" 2
echo "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\2\"
I am trying to create a batch file that will delete images with specific names. The images will have names such as
house-200x300.jpg
car-125x250.jpg
So what I need ideally is a regular expression to target files which end in -(Num1)x(Num2).jpg
Also, the images are in various folders and sub folders so I need to do this recursively from the parent folder.
Thanks
del /S *-???x???.jpg
Perhaps you may want to change del by dir /B command at first just to check that there is not any file that have not the specified file name format, but that will be selected by this wild-card.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*.jpg" ^|findstr /i /e /r /c:"-[0-9][0-9]*x[0-9][0-9]*\.jpg"'
) DO (
ECHO DEL "%%a"
)
GOTO :EOF
This should do the job - targeting only those filenames ending with -numXnum.jpg
You'd need to set your own sourcedir
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO DEL to DEL to actually delete the files.
Have you considered using powershell?
Regular expressions in batch files get very bad very fast as the syntax is limited.
My experience is limited, so there may very well be a better solution than this:
#echo off
FOR /F "delims=?" %%i IN ('dir /B /S ^| findstr /R "[^\.]*[0-9][0-9][0-9]x[0-9][0-9][0-9].jpg"') DO (
del /s "%%i" >nul 2>&1
)
I'm redirecting the output as I think that FOR starts to get confused about the output of del.