Batch command to copy files from source to destination [duplicate] - batch-file

This question already has answers here:
Copy Contents From Folder Using Wildcard On The Directory
(1 answer)
xcopy wildcard source folder name to destination
(2 answers)
Using a batch file to copy files with a wildcard in the directory path?
(2 answers)
Closed 3 years ago.
I need to copy set of files from source to destination, below is the command I am using and its working fine. but folder "29749659" is dynamic and its name always changes. Under "unzipped" only one folder will exist.
xcopy /y C:\Nageswar\unzipped\29749659\files\products\Essbase\EssbaseClient\api\include\* C:\Jenkins\jobs\Planning\branches\develop\workspace\planning\Jni\include
Is there any way to write a command

for /d %%a in (C:\Nageswar\unzipped\*) do xcopy /y "%%a\files\products\Essbase\EssbaseClient\api\include\*" C:\Jenkins\jobs\Planning\branches\develop\workspace\planning\Jni\include
should accomplish this - the for /d scans the target directoryname for directorynames and applies each found in turn to %%a

Related

what is windows equivalent of Touch command to modify timestamp of files in some directory, how to achieve by windows batch built-in commands [duplicate]

This question already has answers here:
Touch file to update the date/time of file? (Modified Date) (WindowsXP BatchScript)
(3 answers)
Closed 2 years ago.
I want to develop batch having step that it will update timestamps of all files within some directory.
Below command gives list of all files in directories.
Can we redirect output of this command to some other windows equivalent touch command, so as to update the timestamps for all files?
cmd>dir /B
Log_adminUser_1.log
Log_adminUser_2.log
Log_adminUser_3.log
Log_adminUser_4.log
Log_adminUser_5.log
Log_adminUser_6.log
As a trick for touching all files in a directory, you could use this:
#ECHO OFF
FOR /F "delims=" %%G IN ('DIR /B') DO (
COPY /B "%%G"+,, "%%G" > NUL
)
The COPY /B construct is documented in TOUCH on SS64, which also explains some caveats with it.

using windows batch script to automate unzip task [duplicate]

This question already has an answer here:
Automating task for unzing a gz compressed file using windows batch script [closed]
(1 answer)
Closed 2 years ago.
I have two issues with this code I have attached below,
it doesn't delete the original file after unzip, I want the script to delete the original file from source folder
the converted file is not saved in target folder as set but it creates a tree of the directory in the same folder of the script and saved output there.
Please help me solve this issue
Sample code I have tried
#echo off
set "source=%userprofile%\Desktop\basanta\Automation\a"
set "target=%userprofile%\Desktop\basanta\Automation\b"
FOR %%A IN ("%source%\*.gz") DO (
"%programfiles%\7-zip\7z.exe" x "%%~A" -o"%target%\%%~pA"
del "%%~A" /Y
)
Please help me to write the script as described above
try: del file /f /q
try to enclose -o parameter with a quotes: "-o%target%"

how to use bat file to move files from subdirectories to one folder [duplicate]

This question already has answers here:
ROBOCOPY - Copy folders content to a single folder
(6 answers)
Closed 4 years ago.
I'm trying to use a .bat file to go into a folder, and take all photos within it and its subfolders, and place them all into another directory. I know how to copy the folder exactly, with all subfolders remaining in place when copied with
#ECHO OFF
XCOPY E:\FromFolderNameX C:\toFolderNameY /m /y
but I only want all of the photos to be in one folder in the end, no subfolders. Can this be done with a batch file?
I am assuming that you want to copy (not move) the photos from the subtree starting at E:\FromFolderNameX into the directory C:\toFolderNameY.
I am assuming that by "photos" you mean .jpg files.
The one-line interactive command is
for /r E:\FromFolderNameX %p in (*.jpg) do copy /y "%~p" C:\ToFolderNameY
If instead of JPG files you want to copy all files, just replace *.jpg with *.
If instead of an interactive one liner you want a batch file, the core of the batch file would be
for /r "%~1" %%p in (*.jpg) do copy "%%~p" "%~2"
(%1 is the first positional argument = the top of the subtree from where you want to copy the files. %2 is the second positional argument = the destination directory.)
In production, the batch file would probably check that the directories %1 and %2 exist and are really directories; and it should probably accept an optional third argument giving the pattern of the files to be copied.
Enter for /? to read more about how for /r works.

Get current folder name [duplicate]

This question already has answers here:
Get directory name from path of %CD%
(4 answers)
Closed 5 years ago.
I have a simple batch file that I use to archive files in tar/gzip format. I have placed the batch file in system32 so that I can access it from anywhere.
I open the command window using "shift + right click" in a particular folder where I want the contents of the folder to be archived and enter the name of the batch file (targz.bat). Batch file does the archiving/compressing.
The problem is I use absolute paths. I need a way to get the current directory and the name of the current folder. I can get the current directory with %cd%, but how do I get the folder name?
For example:
set currentdir=%cd% "C:\xampp\htdocs\wordpress"
set currentfoldername= should be just "wordpress"
Actual code:
#echo off
cd "C:\Program Files\7-Zip"
7z a -ttar "C:\xampp\htdocs\wordpress\archive.tar" "C:\xampp\htdocs\wordpress\*"
7z a -tgzip "C:\xampp\htdocs\wordpress\archive.tar.gz" "C:\xampp\htdocs\wordpress\archive.tar"
del "C:\xampp\htdocs\wordpress\archive.tar"
exit
Desired:
#echo off
set currentdir=%cd%
set currentfoldername=
cd "C:\Program Files\7-Zip"
7z a -ttar "%currentdir%\%currentfoldername%.tar" "%currentdir%\*"
7z a -tgzip "%currentdir%\%currentfoldername%.tar.gz" "%currentdir%\%currentfoldername%.tar"
del "%currentdir%\%currentfoldername%.tar"
exit
Using the ~n modifier you can easely get the last element of a path :
for %%a in (%cd%) do set "currentfoldername=%~na"

How to copy a folder which is contain date formate(DDMMYYYY) as folder name [duplicate]

This question already has answers here:
Batch file to create Folder based on Date and Copy Files from One Location to Another Location?
(1 answer)
Batch process to move file having Date in YYYYMMDD format from one folder to another folder
(2 answers)
Closed 9 years ago.
How to copy a folder which is contain date formate(DDMMYYYY) as folder name and folder name will differ from month to month. How can I create batch file for this.
#echo off &setlocal
set "startfolder=."
set "targetfolder=C:\destination"
cd /d "%startfolder%"
for /f "delims=" %%a ('dir /ad /b ^|grep -E "(([12][0-9]|0[1-9])02|(30|[12][0-9]|0[1-9])(0[469]|11)|(3[01]|[12][0-9]|0[1-9])(0[13578]|1[02]))[0-9]{4}"') do (
md "%targetfolder%\%%~nxa"
copy "%%~a" "%targetfolder%\%%~nxa"
)
findstr doesn't have enough Regex capabilities, you need grep for Windows.

Resources