Copy and rename XLSX file using batch script - batch-file

I have many XLSX files that I will need to copy to a new location and rename. The Excel files appear to be copied to the new location, but when I open them, I keep getting the "found unreadable content" error message, and all the data is gone.
I use an asterisk to find the file, and then I want to rename it so that it doesn't have the date string.
This is the code I'm using:
set OrigLocn=C:\OldLocation\
set NewLocn=C:\NewLocation\
copy "%OrigLocn%S5820-003-terms-sg*" "%NewLocn%S5820-003-terms-sg.xlsx"
copy "%OrigLocn%S5921-293-terms-addp*" "%NewLocn%S5921-293-terms-addp.xlsx"
copy "%OrigLocn%S5921-293-terms-sg*" "%NewLocn%S5921-293-terms-sg.xlsx"
copy "%OrigLocn%S5921-349-terms-addp*" "%NewLocn%S5921-349-terms-addp.xlsx"

I would use the xcopy command to copy over multiple files then use REN to change the name.
XCOPY: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true
REN: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true

change copy to copy /b. This copies in binary mode but the default is ascii mode which terminates the copy when certain binary sequences are encountered.

Using the wildcard in the copy command will cause the target file to be concatenated if more than one file matches the filespec.
if this is the case then you'll need different code to remove the date string.

Related

How do I get cmd to ‘combine’ multiple files from a windows backup?

I am trying to extract a .pst file from a windows backup. In order to do this I need to copy each ‘partial’ file from the backup zips and then combine them together to make the one file. I have a command that will copy them out and combine them from this post but the problem I have is that cmd is not doing it in numerical order, therefore the file is not complete. I am using this script to put the files in order:
Echo y | for /F "tokens=*" %A in (filenamesinorder.txt) do copy /b %A “c:\pstcombiner\combined.pst”
But all this does is copy each individual file and overwrites it. I get that that’s what the command does but I need it to combine all the files into one. What am I doing wrong?
Form the Microsoft documentation for the copy command:
To append files, specify a single file for destination, but multiple files for source (use wildcard characters or file1+file2+file3 format).
You’ll need to construct the source text for the copy in your for loop and do the copy after; I’ll see if I can provide an example when I am at my Windows system.
Instead of concatenating, you could try merging using the type command:
create an empty target file
copy nul target.ext > nul
then loop the type command to merge the files to the end of the target file
type fileN.ext>>target.ext
where fileN.ext is file 1, 2,3 ... n

Using cmd to move files with a specific filename mask

I am trying to use a simple windows cmd command to move a set of .csv files from one directory to another.
I know that this can be easily achieved through:
MOVE "*.csv" "D:/lorik_home"
The issue comes when I want to only move some specific files with a filename mask, for instance, I want to move all files which are in the .csv extension and start with the name: lorik_files_. I already tried using:
MOVE "lorik_files_*.csv" "D:/lorik_home"
This works when there is only one file of the format: lorik_files_20112233_09_33_33.csv, but when there are two files with the masks such as:
lorik_files_20112233_09_33_33.csv
lorik_files_20112233_10_23_42.csv
I get an error such as:
A duplicate file name exists, or the file cannot be found.
I need a hand for capturing those filename prefix masks, conditioned by .csv extension at the end.

How do I copy existing files and create a duplicate files using xcopy?

I can't seem to find a way to copy an existing file but I do not want to let the previous file with the same name get overwritten. Let say I want to copy data.txt from various pc's by using batch file but I do not want "data.txt" gets overwritten, instead is there a way for the command to generate automated filename to avoid it from overwritten?
You can use the "/-y" switch to confirm before copying a duplicate file.
/-y : Prompts to confirm that you want to overwrite an existing
destination file.
The entire list of switches can be found here

Copying csv in batch file causes file to be corrupted

I use the following command to copy a csv (with the date in the name) to a directory called "read":
copy "C:\Users\Brock\Documents\Dropbox\dir\test\file????????.csv" "C:\Users\Brock\Documents\Dropbox\dir\test\file\read\file.csv"
When it is copied over there is some sort of invalid character (looks sort of like "->") that is causing Salesforce DataLoader to be unable to read the file.
Why is my file being corrupted and how can I prevent this from happening?
copy "C:\Users\Brock\Documents\Dropbox\dir\test\file.csv" "C:\Users\Brock\Documents\Dropbox\dir\test\file\read\file.csv" /a /v
Try This...
The CMD environment is detecting that the file is an ASCII file and is adding the ASCII EOF character (→) [0x26d, 0x1Ah] to the end of the newly copied file.
If you add the /b switch to the copy command, it will copy the file using binary mode and won't add the ASCII EOF character to the end of the copied file.
copy /b "C:\Users\Brock\Documents\Dropbox\dir\test\file????????.csv" "C:\Users\Brock\Documents\Dropbox\dir\test\file\read\file.csv"

How to exclude a specific file from being copied using xcopy

I'm new to Windows batch files, but I'm writing a .bat file that just copies a bunch of files from one place to another maintaining the file directory structure. Using xcopy this is simple but I need to exclude some files from being copied. You can use /exclude and create a text file full of strings that you want to be excluded, but this doesn't just exclude files with the exact names in the text file, it excludes all files whose filenames contain any of the strings in the text file.
What this means is, if I want to exclude any files named 123.txt and put this string in my exclusions text file, if there was a file called 1123.txt anywhere in the source folder or any of its subfolders that would also be excluded.
How can I exclude only files with a specific filename from being copied?
Evening Bill.
Can you add a slash before each file name? That should work
EG
instead of
123.txt
blah.txt
use
\123.txt
\blah.txt
Try creating a temporary folder, xcopying all of the files into that folder, deleting the ones you don't want, then xcopying those to the final destination. Finally, delete the temporary folder and its contents with rd xyzzy /q/s

Resources