Batch script to read and extract details from a text file - windows

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

Related

How to batch copy files based on a list (txt) in to another folder with same directory structure?

I have a root directory with over 25,000 files in it. These files are in loads of different subdirectories.
I also have a text file with 4300 lines in it, each line is an absolute path to one of the files in the directory. Like below,
c:\dir1\hat1.gif
c:\dir1\hat2.gif
c:\dir1\dir2\hat1.gif
c:\dir1\dir2\hat2.gif
c:\dir1\dir3\cat.zip
c:\dir1\dir3\banana.exe
I also have another root directory witch is a copy of the original root directory structure but all the directories are empty.
I would like to copy all the files listed in the text file to the directory which is empty and place all the copied files inn the respected subdirectories.
if I use the following batchfile I keep getting file overwrite prompts because it is not copying the files to the correct directories.
#echo off
set dst_folder=c:\DSTN2
for /f "tokens=*" %%i in (USEDFILES.txt) DO (
xcopy /S/E "%%i" "%dst_folder%"
)
How do I modify this so the files are copied to the correct directory?
Since you are copying specific files from a list, you need to make sure the directory structure exists in the destination if you want it in a similar folder structure. So using the power of the FOR command modifiers you can get the file path only from the file name in the file list. You will use that modifier to create the destination directory and also use it as the destination for the XCOPY command.
I have taken the liberty of providing best practices for all the code you are using.
#echo off
set "dst_folder=c:\DSTN2"
for /f "usebackq delims=" %%G in ("USEDFILES.txt") DO (
mkdir "%dst_folder%%%~pG" 2>NUL
xcopy "%%~G" "%dst_folder%%%~pG"
)

Converting multiple PDF files to Text files in subdirectory using Ghostscript in Windows

OS: Windows 10 Professional
I'd like to convert multiple PDF files to text files in main and sub-directories using GhostScript on Windows command line. Here's my command below:
gswin64c -sDEVICE=txtwrite -o test1.txt "test1.pdf"
This code converts "test1.pdf" to "test1.txt" using Ghostscript but I would like to perform the following:
Look for all pdf files in main directory and all sub-directories
Execute Ghostscript on all PDF files
Give the same file name to the output text file. (test1.pdf -> test1.txt)
I appreciate your time and consideration on this!
Build a batch or .cmd file like this:
#echo off
REM Replace these with your actual location
D:
cd "\Main Directory"
for /R %%F in (*.pdf) do call :DOPDF "%%F"
goto ENDIT
:DOPDF
for %%X in (%1) do set PDF_TXTFNM=%%~dpnX.txt
gswin64c -sDEVICE=txtwrite -o "%PDF_TXTFNM%" %1
goto :EOF
REM Clean up
:ENDIT
set PDF_TXTFNM=
EDIT TO ADD:
Side Note: If you do not wish to change your working directory to the main directory, be aware that for /R %%F in ("D:\Main Directory\*.pdf") ... will only work if there actually is a .pdf file in D:\Main Directory. The two main workarounds are to make it the current working directory (the solution chosen in my example) or to force a dummy .pdf file to exist in that directory and then choose to not process it in the subroutine using an IF statement. Holler if you need an example of that latter technique.

Need batch program that moves files without copying then deleting them

Every month I have a very large number of files (in many subfolders) in a particular folder. I need to move them all into a different folder. In an attempt to automate the process of moving them I used robocopy in a batch file. It works fine, but takes HOURS to run. (It is many many GB).
Now, if I do it manually in Windows Explorer, by opening said folder, selecting all, and right-dragging to destination folder, and choosing "Move Here", it moves INSTANTLY. (Windows XP must be pruning and grafting the directory entries, without ever making a second copy of the files. ... and yes, source and destination are on same partition.)
So, QUESTION IS: Does anyone know of a program I can run from a batch file to move files in this instantaneous way? (need to move entire sub-folder tree)
You can use MOVE for this:
C:\>MOVE /?
Moves files and renames files and directories.
To move one or more files:
MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination
To rename a directory:
MOVE [/Y | /-Y] [drive:][path]dirname1 dirname2
[drive:][path]filename1 Specifies the location and name of the file
or files you want to move.
destination Specifies the new location of the file. Destination
can consist of a drive letter and colon, a
directory name, or a combination. If you are moving
only one file, you can also include a filename if
you want to rename the file when you move it.
[drive:][path]dirname1 Specifies the directory you want to rename.
dirname2 Specifies the new name of the directory.
/Y Suppresses prompting to confirm you want to
overwrite an existing destination file.
/-Y Causes prompting to confirm you want to overwrite
an existing destination file.
The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line. Default is
to prompt on overwrites unless MOVE command is being executed from
within a batch script.
For example:
C:\Users\test>mkdir to
C:\Users\test>move from\*.txt to
C:\Users\test\from\1.txt
C:\Users\test\from\2.txt
2 file(s) moved.
With a point in the right direction from #psmears, and a lot of googling, I found the (or a) solution:
#echo off
setlocal ENABLEDELAYEDEXPANSION
REM ** Change to source dir
L:
cd "L:\Backups\Source"
REM ** Move files recursively
for /R %%G in (.) do (
set mydir=%%G
set mynewdir=!mydir:~18!
md "L:\DEST\!mynewdir!"
cd "L:\DEST\!mynewdir!"
move "%%G\*.*" .
)
REM ** Remove now-empty sub-dir structure inside source
L:
cd "L:\Backups\Source"
for /f "usebackq delims=" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d"

Merge all files from the current folder and all subfolders in one file using .bat file

I'm looking for a batch file that merges the content of all files in the current directory and files from all subdirectories.
Also, that would be perfect if files were separated by several new lines in the big file and probably contain the filename with filepath above the each file content.
For example, there are two files in the current folder D:\ , where our batch file is located:
1.
d:\file1.txt contains:
some
text here
hahaha
2.
d:\folderabc\file2.mp3 contains:
doremi text
I run merge.bat file on d:\ and it creates a merge file result.txt (or whatever extension) with such content:
=========d:\file1.txt=========
some
text here
hahaha
=========d:\folderabc\file2.mp3=========
doremi
I appreciate if someone share his solution for this problem.
Thank you so much.
Test this with your folder. It assumes the files are text files.
edit: This has a fix - and you can remove the "d:\base\folder" to run it from the current directory.
#echo off
for /r "d:\base\folder" %%a in (*) do (
(
echo =========%%a=========
type "%%a"
echo(
echo(
echo(
)>>"%temp%\temp.file"
)
move "%temp%\temp.file" . >nul
echo done
pause

How to use a batch file to delete a line of text in a bunch of text files?

I have a bunch of txt files in my D drive which are placed randomly in different locations. Some files also contain symbols. I want a batch file so that I can delete their specific lines completely at the same time without doing it one by one for each file and please refer to a code which does not create a new text file at some other location with the changes being incorporated i.e. I do not want the input.txt and output.txt thing. I just need the original files to be replaced with the changes as soon as I click the batch file.
e.g
D:\abc\1.txt
D:\xyz\2.txt etc
I want both of their 3rd lines erased completely with a single click and the new file must be saved with the same name in the same location i.e. the new changed text files must replace the old text files with their respective lines removed. Maybe some sort of *.txt thing i.e i should be able to change all the files with the .txt extensions in a drive via a single batch file perhaps in another drive,not placing my batch file into each and every folder separately and then running them. Alternatively a vbs file is also welcomed.
This uses a helper batch file called findrepl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697
Place findrepl.bat in the same folder as the batch file below.
It will search for every *.txt file on drive d: and remove line 3.
#echo off
for /r "d:\" %%a in (*.txt) do (
echo processing "%%a"
type "%%a"|findrepl /v /o:3:3 >"%%a.tmp"
move "%%a.tmp" "%%a" >nul
)
pause

Resources