I'm running xcopy within a Jenkins build.
I have the following directory structure:
Ensure\
a
Web_ERP
b
c
Web_ERP
Web_ERP
Web_ERP_Claims
Web_ERP_Finance
I'm trying to copy all folders which start with "Web_ER*" under Ensure (depth=1) to my current workspace.
node () {
stage ('Setup') {
deleteDir()
bat '''
IF NOT EXIST c:\\deploy mkdir c:\\deploy
cd ..\\Ensure
xcopy Web_ER* "%WORKSPACE%" /e
'''
}
}
In reality, all the folders which start with Web_ER* are being copied along with Web_ER* folders which reside under folders a and c.
I want only Web_ER* folders which reside under Ensure to be copied along with their content.
I've tried the following switches with xcopy: /i /e /s /m but I get the same result every time.
Edit #1:
#magoo when I run the command you gave it copies all the files which are under each one of the Web_ER* folders without the folders themselves, I want to copy all the folders strating with "Web_ER*" with all their subdirectories to my destination folder.
Following your example I've also tried:
for /f "delims=" %%a in ('dir /b /ad web_er*') do xcopy ".\\%%a" "%WORKSPACE%" /e /y
But to no avail.
In linux for example it's as easy as:
cp -R folder/pattern* destination/dir
I'm looking for the equivalent command in windows.
What am I doing wrong?
for /f "delims=" %%a in ('dir /b /ad web_er*') do xcopy ".\%%a\*" "%WORKSPACE%" /e
should accomplish this. Process a directory list in /b basic (name-only) form /ad of directorynames matching web_er* in the current directory, and apply each name found in turn to %%a; then xcopy all of the files.
I ended up using the following powershell command:
Get-childitem | where {$_.name -like "Web_ER*"} | copy-item -recurse -destination ..\Ensure-Publish -force
Related
I use gallery-dl to download pics from Pixiv.com. All of the below are run from Windows' .bat files.
I manually create a folder with only {user[id]}.
Example: Pixiv - Temp\24517
I then copy gallery-dl.bat (which calls on gallery-dl.exe and the related gallery-dl.conf, elsewhere) and a number of assisting .jpg files into that folder.
Example: Pixiv-Temp\24517\gallery-dl.bat
I've done this for over 5,000 users.
Running gallery-dl.bat from within folder configured for the below, passing a variable of the folder name will download the pics as well as saving a *.sqlite3 file to the folder 24517.
gallery-dl.conf
"directory": ["{user[id]}"],
The actioning part of gallery-dl.bat (listed in whole, below).
"C:\Users\AdminS6\gallery-dl\gallery-dl.exe" -d "..\..\." --download-archive "%~dp0GDB.sqlite3" "https://www.pixiv.net/en/users/%FOLDER%/illustrations"
The current .bat file in whole:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0" || exit /B
move /y "..\Folder2.jpg" "..\Folder.jpg"
for %%I in (..) do set "FOLDER=%%~nxI"
for /f "tokens=1 delims= " %%a in ("%FOLDER%") do set tmpFOLDER=%%a
"C:\Users\AdminS6\gallery-dl\gallery-dl.exe" -d "..\..\." --download-archive "%~dp0GDB.sqlite3" "https://www.pixiv.net/en/users/%FOLDER%/illustrations"
del "..\Folder3.jpg"
if not exist "GDB.sqlite3" move /Y "..\Folder4.jpg" "..\Folder.jpg"
if exist "GDB.sqlite3" del "..\Folder4.jpg" && del "..\Folder.jpg"
if exist "..\*.mp4" md "..\Reels" & move /y "..\*.mp4" "..\Reels\"
if exist "..\*.gif" md "..\GIFs" & move /y "..\*.gif" "..\GIFs\"
if exist "..\*.webp" md "..\WEBPs" & move /y "..\*.webp" "..\WEBPs\"
robocopy "..\..\%FOLDER%" "X:\11Web\gallery-dl\Pixiv - Temp\24517 -" *.* /e /move
TIMEOUT /T 1
popd
endlocal
I now plan to change gallery-dl.conf to {user[id]} - {user[name]}. This will result in a change to the destination folder being created now by gallery-dl. The original folder is left unchanged.
Example my original source folder: Pixiv-Temp\24517
Example gallery-dl new output folder: Pixiv-Temp\24517 - ケースワベ
gallery-dl.conf
"directory": ["{user[id]} - {user[name]}"],
gallery-dl.exe
"C:\Users\AdminS6\gallery-dl\gallery-dl.exe" -d "..\..\." --download-archive "%~dp0GDB.sqlite3" "https://www.pixiv.net/en/users/%FOLDER%/illustrations"
Not knowing how a {user[name]} will come out in Explorer folder naming, I am left to still manually create an original source folder with only {user[id]}. I will continue doing this due to ease.
The above noted change will present problems I would like to overcome:
When running gallery-dl, the pics from Pixiv will be downloaded to an Explorer folder name of {user[id]} - {user[name]}, while the original manually-created folder still hosts the gallery-dl and *.sqlite3 file. I need these combined.
When re-running/updating gallery-dl against the 5,000+ other folders, I will be left with 5,000 of one {user[id]} and 5,000 of the other {user[id]} - {user[name]}.
Robocopy has shown the most promise in solving these. When run in the below .bat, gallery-dl runs and all files from {user[id]} folder are moved to {user[id]} - {user[name]} folder, leaving {user[id]} folder empty and I just run a script that deletes all empty folders. This has the added benefit that the .sqlite3 files are all moved.
MY ISSUE: I have not yet been successful with Robocopy without having to
explicitly name the path for the destination folder.
Example works:
robocopy "..\..\%FOLDER%" "X:\11Web\gallery-dl\Pixiv - Temp\24517 -" *.* /e /move
Example doesn't work:
robocopy "..\..\%FOLDER%" "..\..\24517 -" *.* /e /move
which after passing a second variable would look more like:
Example doesn't work:
robocopy "..\..\%FOLDER%" "..\..\%FOLDERDESTINATION%" *.* /e /move
nor
I have no idea how to pass a variable from a folder name that is not known in advance {user[id]} - {user[name]}, but must be searched for within the above root folder.
If there was a way from within the .bat file to
Declare a variable that matches the current folder {user[id]} (Already done in the below).
Example: 24517
and
And gallery-dl creates the new {user[id]} - {user[name]} and done its business, the .bat find and declare a variable that matches "{user[id]} -".
Example: 24517 - ケースワベ
As such, I will always know the first half of any variable:
{user[id]} -
or in this case
24517 -
Question is can the entire new folder name be searched for based on the known (Example 24517 -), a suitable secondary variable created equating to the new folder name, from only that?
And is then able to pass this variable to Robocopy in order to successfully move all desired files?
Example:
robocopy "..\..\%FOLDER%" "..\..\%FOLDERDESTINATION%" *.* /e /move
That would be just smashing!
Any and all ideas/assistance are appreciated. Thank you in advance!
Final result after applying successful answer from Stephan
#echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0" || exit /B
move /y "..\Folder2.jpg" "..\Folder.jpg"
for %%I in (..) do set "FOLDER=%%~nxI"
for /f "tokens=1 delims= " %%a in ("%FOLDER%") do set tmpFOLDER=%%a
"C:\Users\AdminS6\gallery-dl\gallery-dl.exe" -d "..\..\." --download-archive "%~dp0GDB.sqlite3" "https://www.pixiv.net/en/users/%FOLDER%/illustrations"
del "..\Folder3.jpg"
if not exist "GDB.sqlite3" move /Y "..\Folder4.jpg" "..\Folder.jpg"
if exist "GDB.sqlite3" del "..\Folder4.jpg" && del "..\Folder.jpg"
if exist "..\*.mp4" md "..\Reels" & move /y "..\*.mp4" "..\Reels\"
if exist "..\*.gif" md "..\GIFs" & move /y "..\*.gif" "..\GIFs\"
if exist "..\*.webp" md "..\WEBPs" & move /y "..\*.webp" "..\WEBPs\"
for /d %%a in ("..\..\%FOLDER% - *") do set "FOLDERDESTINATION=%%a"
rem ECHO %FOLDERDESTINATION%
robocopy "..\..\%FOLDER%" ".\%FOLDERDESTINATION%" *.* /e /move
TIMEOUT /T 1
popd
endlocal
When I understood your question right, this should help:
for /d %%a in ("..\..\%folder% - *") do set "folderdestination=%%a"
(Assumption: there is just one username per %folder% - else this will get only the last of them)
I have a folder structure like:
-MainDir
-f0
-f0
-f0
-a.txt
-b.txt
-c.txt
-f1
-f1
-f1
-aa.txt
-bb.txt
-cc.txt
If you see above my files are in 3rd folder in terms of hierarchy. Now my requirement is to be able to copy only the main folder and it's files to another location. For example: output for above structure should be as following:
-MainDir
-f0
-a.txt
-b.txt
-c.txt
-f1
-aa.txt
-bb.txt
-cc.txt
but when I try using XCOPY, all the folders get copied as it is. So I am trying to figure out a way to achieve my target folder structure as shown above
The following will loop the directories under main, find all files, and put them all in the top most sub directory in destination as you're looking for:
CMD Script:
#(SETLOCAL
ECHO OFF
SET "_MainDir=C:\Main\Dir"
SET "_DestDir=%Temp%\Destination\Main\Dir"
)
FOR /D %%A in (
%_MainDir%\*
) DO (
IF NOT EXIST "%_DestDir%\%%~nxA" MD "%_DestDir%\%%~nxA"
FOR /F %%a IN ('
DIR /A-D /S /B "%%~fA\*"
') DO (
ECHO Copying: "%%~fa"
ECHO To: "%_DestDir%\%%~nxA\%%~nxa"
COPY /B /V /Y "%%~fa" "%_DestDir%\%%~nxA\%%~nxa"
)
)
This is a quite easy task using xcopy together with a for /D loop, given that the three sub-directories in each hierarchy branch are equally named (like f0\f0\f0 and f1\f1\f1):
rem // Loop through immediate sub-sirectories of main directory:
for /D %%I in ("D:\MainDir\*") do (
rem /* Build path to files into second sub-directory (though there are no files);
rem the third sub-directory and its contents is regarded due to the `/S` option;
rem the copy destination directory then receives the whole contents of the
rem second source sub-directory, including the third source sub-directory: */
xcopy /S /I "%%~I\%%~nxI\*.*" "D:\CopyDir"
)
I need to be able to create a bat script to do 3 things:
Search for multiple specific filenames in a directory.
Find the most recently generated version based on each filename specified.
Copy that most file to a new dir.
I am very new to coding in general, so any assistance would be much appreciated.
So far all I have been able to do is figure out how to copy files from one location to another using the below:
xcopy /s c:\source\differentfilename1.csv d:\target\
xcopy /s c:\source\differentfilename2.txt d:\target
xcopy /s c:\source\differentfilename3.html d:\target
So far I have tried the following and its not copying the files over:
ECHO
CD D:\Data\
MKDIR D:\Data\CopyFilesHere
for /R %file in (Filename1.*) DO XCOPY "%file" D:\Data\CopyFilesHere
for /R %file in (Filename2.*) DO XCOPY "%file" D:\Data\CopyFilesHere
for /R %file in (Filename3.*) DO XCOPY "%file" D:\Data\CopyFilesHere
I have since noted there are subfolders I need to search through also.
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"
Is there a way to copy directories recursively inside a .bat file? Is an example of this available?
Look into xcopy, which will recursively copy files and subdirectories.
There are examples, 2/3 down the page. Of particular use is:
To copy all the files and subdirectories (including any empty subdirectories) from drive A to drive B, type:
xcopy a: b: /s /e
After reading the accepted answer's comments, I tried the robocopy command, which worked for me (using the standard command prompt from Windows 7 64 bits SP 1):
robocopy source_dir dest_dir /s /e
You may write a recursive algorithm in Batch that gives you exact control of what you do in every nested subdirectory:
#echo off
call :treeProcess
goto :eof
:treeProcess
rem Do whatever you want here over the files of this subdir, for example:
copy *.* C:\dest\dir
for /D %%d in (*) do (
cd %%d
call :treeProcess
cd ..
)
exit /b
Windows Batch File Looping Through Directories to Process Files?
I wanted to replicate Unix/Linux's cp -r as closely as possible. I came up with the following:
xcopy /e /k /h /i srcdir destdir
Flag explanation:
/e Copies directories and subdirectories, including empty ones.
/k Copies attributes. Normal Xcopy will reset read-only attributes.
/h Copies hidden and system files also.
/i If destination does not exist and copying more than one file, assume destination is a directory.
I made the following into a batch file (cpr.bat) so that I didn't have to remember the flags:
xcopy /e /k /h /i %*
Usage: cpr srcdir destdir
You might also want to use the following flags, but I didn't:
/q Quiet. Do not display file names while copying.
/b Copies the Symbolic Link itself versus the target of the link. (requires UAC admin)
/o Copies directory and file ACLs. (requires UAC admin)