Copying all file contents into new files - Batch - windows

What I need to do is (and I have some idea, just can't get this solidified):
Read all files in a directory (for loop)
In loop, I need to copy the current file in the loop's contents into a new file (x.txt into x2.txt) in the same directory. This can be done with a type >> command.
I'm more confused about the looping part of it. How do I do that?
Edit:
This is my current script:
FOR %%i IN (*)
Do type %%i >> %%i2

This will copy each .txt file to a backup file with extension .txt2:
for %i in (*.txt) do type "%i" > "%i2"
Note that in a batch file you need to double the %:
for %%i in (*.txt) do type "%%i" > "%%i2"
It's not clear from your question if you want all found files dumped into the same resulting text file or if you want one-to-one copies of each.
If you just want one results file, you could also use a variant of the copy command, i.e.:
copy *.txt output.dat

Related

Batch script to read and extract details from a text file

I need to copy a few files and folders to their respective destinations using a Windows batch script.
All the files and folders I am supposed to copy, are kept within a folder, SOURCE.
Example:
folder: C:\X\Y\Z\SOURCE\A
file : C:\X\Y\Z\SOURCE\A.txt
file : C:\X\Y\Z\SOURCE\B.txt
folder: C:\X\Y\Z\SOURCE\ZZZ
The destination paths of all the above are provided as text file contents, destination.txt.
Content of destination.txt:
C:\FinalDestination\D\A\...
C:\FinalDestination\N\A.txt
C:\FinalDestination\C\B.txt
C:\FinalDestination\U\ZZZ\...
Where three dots at the end signifies a directory, otherwise it's a file.
What I need to do in the above scenario is:
Copy folder A from SOURCE to C:\FinalDestination\D\
Copy file A.txt from SOURCE to C:\FinalDestination\N\
Copy file B.txt from SOURCE to C:\FinalDestination\C\
Copy folder ZZZ from SOURCE to C:\FinalDestination\U\
I don't know how to do it as I am pretty new to Windows command line.
I know XCopy is a command which can work for me, xcopy source destination, but I don't know how to extract the source and destination details.
Using an unchanged destination.txt and your supplied data, the following may help:
#Echo Off
Set "sD=C:\X\Y\Z\SOURCE"
Set "sF=destination.txt"
For /F "UseBackQ Delims=" %%A In ("%sF%"
) Do For %%B In ("%%~fA.") Do echo=XCopy/IY "%sD%\%%~nxB" "%%~dpA." 2>Nul
Pause
You need only modify the content of the variables at lines 3 and 4
Note:
I have currently made it so that nothing is copied, just the commands output to your screen. If you are happy with the output remove echo= from line 7 and delete the content of line, 8

Windows Batch script to rename files with it's folder name within a for loop

I have a bunch of .flv files in subdirs and I need to loop through them and rename it according to its path.
I was able to loop through them all, but I don't know how to split the path and rename it using batch script.
Here's what I have so far:
echo off
for /R %%F in (*.flv) do (
echo %%~pF
)
The "echo %%~pF" prints the path for the current file on the loop, something like this:
\folder\morefolders\activity\ NameThatIwant \Videos\
I tried spliting with "delims=\" on my for loop but I get only "echo off".
I tried other tutorials, read other questions on SO but none of them were renaming the files from a split string from the path of the file in the loop.
Could you guys help giving suggestions or direct me to any material that explains those %% codes?
Thanks.
I think you do not need to split the path, though you could do it using for /f "delims=NameInthePath tokens=1", where NameInthePath - is some word in the path and tokens= gives you the first part of the path separated by delims.
Actially, if you need to rename file name you need to use REN command. If you need to change the path for the flv file - use copy of move command.
A batch file to rename all .LOG files to .TXT in the 'demo' folder and all sub-folders:
CD C:\demo\
For /R %%G in (*.LOG) do REN "%%G" "%~nG.TXT"

batch file to list all txt files from other directories

I am in the process of fetching all the .txt files from one directory to another (my current).
My current directory is
C:\USERS\MRAH
where i have the batch file and i have the code to fetch all .TXT files from the directory
dir E:\S_RUNS\12 month_STAR\S_2013\tst\*.txt /b >> INPUT_FILE_LIST.TXT
I am not able to fetch all the .TXT file which are in the E:\ DIREC into INPUT_FILE_LIST.TXT file on C:\USERS\MRAH
Can anyone let me know as to what should be code to fetch all the .txt file from one directory to another...
Thanks!
I'm not completely sure this will work on multiple directories but you could try it.
Cd E:\[path]
for /d %%a in (*) do (if %~xa == .txt echo %%a >> input_list.tmp)
for /f %%a in (input_list.tmp) do (copy %%a C:\USERS\MRAH)
note the batch file needs to be run from the E:[path]
also note you save it as a .tmp file to prevent it from logging itself
also instead of making an input_list file do it directly:
for /d %%a in (*) do (if %~xa == .txt copy %%a C:\users\MRAH)
Tell me if this doesn't work
Yours, Mona
Assume your current working directory is c:\testDir and you wanna copy all txt files from c:\source to d:\dest then use following content in a batch file
copy c:\source*.txt d:\dest

Renaming files overwrites part of the filename

I ahve a directory full of files, and I want to rename each of them to have TA_ in front of the original file name. file1.txt should be renamed to TA_file1.txt. What I am getting is TA_e1.txt instead.
ren "c:*.txt" "TA_*.txt" is the command I am trying to use.
The file names are all of various lengths, and no matter what I try, it always overwrites the first 3 characters of my file name....
A simple one liner would be:
for %i IN (*.txt) DO ren "%i" "TA_%i"
This loops over all files (*.txt) and passes their name in the %i variable to the ren command. ren can then use the %i content to expand it with your desired prefix.
The command will only work for files in the current directory. For more complex things you should write a batch file. Come back if you need help with that.

How to blank files in batch

How would I go about reading an entire directory and blanking files with a specific extension? I have an application that reads the content of a specific folder and returns an error if a file is missing, however it does't check to see if the files are valid, so I want to make them NULL to get around the checks.
if by 'blanking' you mean truncating them, you could use the following:
for /f %%a in ('dir *.[my ext]') do (echo . > %%a)
note that the double % is for use within a batch file. if you are running this from a command line, use a single %.
EDIT:
to incorporate #Loadmaster's improvement:
for /f %%a in ('dir *.[my ext]') do (type nul > %%a)
First, create an empty file. Call it "blank". You can use Notepad to just save an empty file, for example.
Let's suppose the specific extension is ".xyz". Run this:
for %f in (*.xyz) do copy /y blank %f
The for loop sets variable "%f" to each file name in turn, and runs the copy command. The copy command copies the blank file on top of each matching file.
By the way, you can find out more about the for command using the help command:
help for
You could make the batch file take the filter and then it's much more useful.
#for %%i in (%1) do cd.>%%i
Usage (if you call it empty.bat):
empty *.c

Resources