Create a batch file to copy and rename file - command-line

I need to write a batch file that copies a file to a new folder and renames it.
At the moment, my batch file consists of only this command:
COPY ABC.PDF \\Documents
As you can see, it only copies the file ABC.pdf to the network folder Documents.
However I need to change this so it renames the file ABCxxx.pdf, where xxx is a text variable that I would like to set somewhere in the batch file.
For example, if xxx = _Draft, then file would be renamed ABC_Draft.pdf after it is copied.

Make a bat file with the following in it:
copy /y C:\temp\log1k.txt C:\temp\log1k_copied.txt
However, I think there are issues if there are spaces in your directory names. Notice this was copied to the same directory, but that doesn't matter. If you want to see how it runs, make another bat file that calls the first and outputs to a log:
C:\temp\test.bat > C:\temp\test.log
(assuming the first bat file was called test.bat and was located in that directory)

type C:\temp\test.bat>C:\temp\test.log

Related

Batch file copy command partial name on file to directory

I have several files in a folder C:/Folder1 and I would like to create a batch file with copy or move command, to copy/move the files containing a partial number,12345, to C:/Folder2. The partial number it not at the beginning or end of the file name but in the middle.
I tried with wildcards but doesn't work.
Copy or Move ***.pdf C:/Folder2
Can anyone help please ?
Thanks,
Rui

Zip files using 7zip from command window

Using 7zip, I want to give a location of a folder of files something like...
D:\Home\files Then I want it to zip all the files in that folder and leave them there. So for example zipme.txt become zipme.zip, so all files would keep their name but just become a zip file. I have tried to use
FOR %i IN (D:\Home\files) DO 7z.exe a "%~ni.zip"
But when I do it it adds a zip file for the directory so my output would be in the correct folder but would contain
D:\Home\files\file.zip
D:\Home\files\zipme.zip
zipped files also all items in directory like..
zipme.txt
zipme2.txt
D:\Home\files/zipme2.zip
So how can I zip each file individual in a folder and have the new zipped name be the individual files name
Was able to get this to work.
FOR %i IN (D:\Home\files\*) DO 7z.exe a "%~ni.zip" "%i"

How can I copy a file in Powershell without locking the source file which is being copied?

Is there a way to use the "Copy" command in Powershell to copy a source file to a destination folder but not lock the actual file being copied?
Reason for asking is the file being copied is also an input file for a separate process and if the process starts while the Powershell script is running then it will fail if the Powershell script has locked the input file it uses.

Winrar CommandLine & Powershell : Exclude Full Directory Path Structure

Suppose I have a directory structure like
C:\Users\Desktop\abc\d
I want to rar archive the abc folder so that the structure of rar is:
abc\d
When I try using powershell to archive, winrar replicates the full path inside the archive, like:
\Users\Desktop\abc\d
I dont want the full path to be created inside the archive
Here's the script:
https://gist.github.com/saurabhwahile/50f1091fb29c2bb327b7
What am I doing wrong?
Use the command line:
Rar.exe a -r -ep1 Test.rar "C:\Users\Desktop\abc"
Rar.exe is the console version of WinRAR stored in same directory as WinRAR.exe. You can use this command line also with WinRAR.exe if you want to see the compression process in a graphic window.
a is the command and means add files to archive.
-r is a switch to recursively add all files and subdirectories including empty subdirectories to the archive.
-ep1 is another switch which results in execluding base directory.
For this command line the base directory is "C:\Users\Desktop\" and therefore the created archive Test.rar contains only abc and all files and subdirectories in directory abc which is what you want.
One more hint: Using the command line
Rar.exe a -r -ep1 Test.rar "C:\Users\Desktop\abc\"
results in adding all files and subdirectories of directory abc to the archive, but without directory name abc being also stored in the archive. The backslash at end makes this difference.
In other words: On using switch -ep1 everything up to last backslash in file/directory specification is not added to the archive.
For more information about available switches see the text file Rar.txt in the program files directory of WinRAR.

Copy .csv files in to one file with file name printed to allinone.csv

I have been given a data extract dump from an Oracle DB (cc&b). Each extract comes with a .log file which gives a table def, pk, fk, data types etc. I want to copy all these files in to one. I aware of the MS-Dos command
'copy *.log allinone.txt'
My problem is the content of the .log files do not contain the table name; the table name only exists on the file name. I need the table name printed in the allinone.txt file.
There are 486 tables so manual is not really ideal. Is there way to Print File Name + Content?
I'm assuming you are really using Windows, and not MS-DOS.
I'm also assuming all the .log files are in the same folder.
No batch file is required. Simply CD to the folder with the log files. Then run the following command:
type *.log >combinedLog.txt 2>&1
The TYPE command will include the file name at the top of each file output when a single command types multiple files. The file name is printed to stderr, so stderr is redirected to stdout (2>&1).