Copying a Folder and renaming it using command prompt - batch-file

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

Related

How to differentiate between system and user files in the batch language?

I am looking to copy all text files on the C drive, but the program needs to avoid text files that are part of the system, and only copy the text files that have been created by the user.
Is there a built in way to do this, or do I need to get creative? If so, how would you go about it? I would like to keep it within batch and not involve powershell or anything like that.
I thought about using creation dates to determine whether or not a file is a system file, but that didn't work all the way.
You can generate a list of all systemfiles using the dir command. So
dir /b /as > %USERPROFILE%\excluded_Files.txt
Would generate a list of the systemfiles from that directory.
Using xcopy as described here you can now copy all files from one directory to another excluding the systemfiles.
The xcopy command should be something like this:
xcopy C:\firstDirectory\*.txt C:\secondDirectory /EXCLUDE:%USERPROFILE%\excluded_Files.txt
Edit
So your code that is there would copy all rtf and txt files from the whole C drive to Drive D folder E.
A batch-file using my method would look like this:
#echo off
cd /d C:\
dir /b /as /s > %USERPROFILE%\excluded_Files.txt
xcopy C:\*.rtf D:\E /EXCLUDE:%USERPROFILE%\excluded_Files.txt /s
xcopy C:\*.txt D:\E /EXCLUDE:%USERPROFILE%\excluded_Files.txt /s
Explanation:
#echo off supresses redundant output of the commands that would be displayed before execution.
cd /d C:\ changes the current execution directory to the root of the C drive. /d is added for potential drive change.
dir /b /as /s > %USERPROFILE%\excluded_Files.txt redirects the output of the dir command into a file in your userprofile (Usually C:\Users\yourUserName). The switch /b will only show the filenames and not size, creation date etc. /as will only list system files and /s makes the dir command work recursively through all directories.
xcopy C:\*.rtf D:\E /EXCLUDE:%USERPROFILE%\excluded_Files.txt /s will copy all rtf files from C:\ to D:\E and exclude all files from the list previously created (that will contain all system txt and rtf files (as well as others)). /s makes xcopy work recursively as for dir.
Next line is a copy of the same with txt files.

BAT File to find most recent files based on filename

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.

batch file xcopy to a partially unknown directory with a dot ( . ) in

Im trying to create a batch file that will copy a directory to another directory however, the directory I wish to copy too has a portion of the name randomly generated i.e. "jibberish.Directory1".
is there a way to do this ?
I was trying something like:
XCOPY "%~dp0DATA\test" "%APPDATA%\Application1\Directory\*Directory1\" /E /C /R /I /K /Y
but total fail. Any help would be greatly appreciated :) Thanks!
xcopy does not support wildcards in directory names. Therefore a for loop is additionally necessary for this task which is as follows:
In folder C:\Temp there is the batch file DuplicateFolder.bat with the code below.
The folder to duplicate is DATA\test in directory of the batch file.
C:\Temp\DATA\test contains:
SubFolder1
FirstFile.txt
Second File.txt
Sub Folder 2
ListFile.csv
Readme.txt
All these subfolders and files should be copied to each subfolder in %APPDATA%\Application1\Directory ending with a number after a dot.
%APPDATA%\Application1\Directory contains for example:
Folder abc.1
Folder e-g.2
Folder h to k.3
Folder last.4
Folder not changed
The last folder should be ignored as it does not contain a dot and a number at end.
The result should be for example for the first subfolder Folder abc.1:
Folder abc.1
SubFolder1
FirstFile.txt
Second File.txt
Sub Folder 2
ListFile.csv
Readme.txt
Code of DuplicateFolder.bat is:
#echo off
set "SourceFolder=%~dp0DATA\test"
for /D %%F in ("%APPDATA%\Application1\Directory\*") do (
if not "%%~xF" == "" %SystemRoot%\System32\xcopy.exe "%SourceFolder%" "%%~F" /E /C /Q /R /I /K /Y >nul
)
set "SourceFolder="
This batch file just checks if folder name contains a dot without checking if string after dot - the "file extension" referenced with %%~xF - is really a number. This very simple test is very fast.
For details about used command for and an explanation of the parameters and special loop variable references open a command prompt window, execute for /?, and read help output for this command in the console window.

batch file command to copy file

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.

xcopy does not create directory structure

I have a strange problem with xcopy in Windows XP Professional. I don't know if its a stupid question as I am specifying only a file as the source, so should I even expect any other behavior ? This is it:
I am using xcopy <src> <dest> /s/y.
<src>=C:\sourcefolder\a\b\c\d\something.java and
<dest>=C:\destinationfolder.
Now xcopy copies the file but does not create the directory structure \a\b\c\d\ inside C:\destinationfolder .
what I want is C:\destinationfolder\a\b\c\d\something.java and
what I get is C:\destinationfolder\something.java
I have tried to run it in destination folder C:\destinationfolder by specifying a . for target folder
Tried it without any target in above
There is a script I have which calls xcopy iteratively so I am left with C:\destinationfolder\many java files without any directory structure.
A. Yes I have done xcopy /? to see all options
B. /T also does not create any empty directory structure
C. I can not go to source folder a\b\c\d\ and run xcopy . <dest>
UPDATE
I removed my previous answer on using ROBOCOPY. I believe the following will do what you want using XCOPY.
Assuming your folder structure is like this:
SOURCE = C:\MyJavaStuff\A\B\C\D\something.java
DEST = C:\MyDestination
Run XCOPY like this:
XCOPY C:\MyJavaStuff\something*.java C:\MyDestination /S /E
Note the * in something*.java.
The problem is that you are specifying which file to copy in the source. xcopy won't create the folder structure in this case. However, if you change your call to xcopy to
xcopy *.java C:\myfolder /s/y
it will copy the .java files and the folder structure as well. You need to specify a wildcard for this call to work as you want. If you want only to copy specific files, you will have to adjust the call to xopy, e.g.:
xcopy something.jav* C:\myfolder /s/y
Edit
You say that you get the list of files to copy from another command. If you can output this list of files in a text file, you could do the following:
FOR /F "tokens=* delims=," %F in (d:\test\list.txt) DO xcopy src\%~nxF* .\dest /S /Y
What this command does is read a text file ("d:\test\list.txt" in this case), read every line, and for each file, run xcopy, adding a wildcard at the end of the file name to make sure it creates the folder structure.
I'm assuming here that:
You can get the list of files in a text file, with only the file names (and optinally the paths)
You know the source folder ("C:\sourcefolder" in your example, the folder structure "a\b\c\d" does not need to be known) and can use it in the FOR command.
You can also use the following form:
FOR /F "tokens=* delims=," %F in ('cmd') DO xcopy src\%~nxF* .\dest /S /Y
where cmd needs to be replace with the command you use to generate your list of files to copy.
Note that if you use this FOR command in a batch file, you need to replace %F with %%F (and %~nxF* with %%~nxF*).
I had a look at the xcopy switches and you can copy the directory structure with /T, although that doesn't copy empty directories you can override this with /E. So your command would look like this:
xcopy C:\sourcefolder\a\b\c\d\something.java C:\destinationfolder /T /E /S /Y
Hope this helps!
In order to get C:\destinationfolder\a\b\c\d\something.java XCOPY needs to know how much of C:\sourcefolder\a\b\c\d\something.java to duplicate.
You can use:
C:
cd \sourcefolder
XCOPY something.java* C:\destinationfolder\ /S
Just be aware that this may have the side effect of also copying C:\sourcefolder\oops\something.java to C:\destinationfolder\oops\something.java as well as any other matches for something*.java under C:\sourcefolder\.
It seems to me that xcopy is typically used for copying directory trees, not single files (though it can work). And, xcopy will recreate the directory structure under the source folder in the target folder. If xcopy is given the /i switch, the target folder is assumed to be a directory. It will be created if it does not exist, even if there are multiple parents that need to be created.
You have C:\MyJavaStuff\A\B\C\D\something.java - that is your source. You want to end up with something.java not in C:\destinationfolder, but in C:\destinationfolder\A\B\C\D - so that is your target. You don't even have C:\destinationfolder. That is OK, with /i the entire path will be created.
xcopy /i c:\MyJavaStuff\A\B\C\D\something.java C:\destinationfolder\A\B\C\D
If something.java were the only file under C:\MyJavaStuff, you could also use
xcopy /sei c:\MyJavaStuff C:\destinationfolder
That would recreate the entire tree structure, copying your file. But if there are other files (and folders) under MyJavaStuff they would also be copied.
I have written a very similar batch file using xcopy. Perhaps what I did will help you.
This is the command I used:
xcopy "c:\Data Files\Dave's Data\*.*" "m:\Dave's Data" /R/D /E/H
In this case, Dave's Data on the source contains an entire directory tree containing at least 50,000 files & exceeding 75GB data. It runs perfectly on Windows XP
I found /T was unnecessary as the directory tree is copied. I also found /S was unnecessary as /E copied directories & sub-directories including empty ones. I included /R to copy & overwrite read only files on the destination. /H copied hidden directories. /D copied only newer files. I use this as a daily backup tool for my data.
The only problem I have is while this command will work on Windows 7 the first time, it will not work on subsequent runs when the destination directory tree exists. I suspect this is due to a privilege issue as the xcopy command will work on subsequent runs on Windows 7 within a cmd.exe window.

Resources