Batch: Copy a list (txt) of files ignoring extension - batch-file

I have the next batch file, but its matching by file name. This time the list I have is huge and has file names without extension, but can I ignore the extension and copy for example: filename.* to the destination folder?
This is the current script:
title Deploying Edithor
set src_folder=S:\ApliTelinver\Compilacion\Edithor 10.5\Pipe
set dst_folder=J:\alazarev\Objetos-Migracion-Pipe
set filelist=filelist-pipe.txt
echo Origen: %src_folder% >> "pipemigracion-!datetimef!.log"
echo Destino: %dst_folder% >> "pipemigracion-!datetimef!.log"
echo.
REM for /f %%i in (%filelist%) DO xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" > "%dd%.log"
for /f "delims=" %%i in (%filelist%) do (
xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" >> "pipemigracion-!datetimef!.log"
)
echo Success. >> "pipemigracion-!datetimef!.log"
echo.
echo Done - Check log pipemigracion-!datetimef!.log
echo.
pause
goto start

Here's how I've understood the question. The file list, filelist-pipe.txt, contains file names, and all of the names are without extensions.
If so, you only need to append .* to the source file path in the XCOPY command:
title Deploying Edithor
set src_folder=S:\ApliTelinver\Compilacion\Edithor 10.5\Pipe
set dst_folder=J:\alazarev\Objetos-Migracion-Pipe
set filelist=filelist-pipe.txt
echo Origen: %src_folder% >> "pipemigracion-!datetimef!.log"
echo Destino: %dst_folder% >> "pipemigracion-!datetimef!.log"
echo.
REM for /f %%i in (%filelist%) DO xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" > "%dd%.log"
for /f "delims=" %%i in (%filelist%) do (
xcopy /S/E/U/Y "%src_folder%\%%i.*" "%dst_folder%" >> "pipemigracion-!datetimef!.log"
)
echo Success. >> "pipemigracion-!datetimef!.log"
echo.
echo Done - Check log pipemigracion-!datetimef!.log
echo.
pause
goto start
Please let me know if I'm still missing something.

Related

How do I combine files in order

I have/found a windows batch script that will combine csv files from all sub-directories. It works great in Windows 10 but when I run the script in Windows 7, all the files are out of order. How do I force the order in which to combine the csv files?
echo #off
for /r %%i in (*.csv) do (
if not %%~nxi == output.csv (
echo %%~nxi >> output.csv
echo %%i
echo %%~nxi
type "%%i" >> output.csv
echo. >> output.csv
echo. >> output.csv
)
)
Just a little change to your code will do the job:
#echo off
FOR /F "usebackq delims=" %%i IN (`dir /s /b /O:N *.csv`) do (
if not "%%~nxi" == "output.csv" (
echo %%~nxi >> output.csv
echo %%i
echo %%~nxi
type "%%i" >> output.csv
echo. >> output.csv
echo. >> output.csv
)
)
Point is to use another command's out -- use dir to control the sorting order.
/b for clean output by dir
/O for ordering, N -- name.
check FOR /? and dir /? for more details.
PS: you may wanna use:
echo %%~dpnxi >> output.csv
instead of line 4's echo %%~nxi >> output.csv , to show full path of each file in your output.csv.
This is just a slight modification to POW's answer that should give you better performance. This technique keeps the file open for writing. When you use the append multiple times, it is opening and closing the output file. So the file pointer has to be reset every time it outputs to the file.
#echo off
(FOR /F "usebackq delims=" %%i IN (`dir /s /b /O:N *.csv`) do (
echo %%~nxi
echo %%i >con
type "%%i"
echo.
echo.
)
)>output.tmp
rename output.tmp output.csv
Here is another possible solution:
#echo off
for /R %%A IN (*.csv) do (
if not "%%~nxA"=="output.csv" (
(echo %%~fA && type "%%~fA" && echo. && echo.)>>output.csv
echo Processed: %%A
echo %%~nxA
)
)
with much less code.
Using /R option with for to loop through subfolders in %cd%.
We want to parse ALL csv files so, we specify it in parenthesis with (*.csv).
Then, we check if csv file currently processed is output.csv.
If not, then we append full path of the csv ("%%~fA") file, its content (type "...") and two newlines (echo.) to output.csv. This condition is not really required, but added to be on-topic with the question. Also, if you don't want to append full path to output.csv, but filename and extension only just replace %%~fA with %%~nxA.
After that, we echo current file processed and its filename and extension.
If file currently processed is output.csv, then we repeat the loop.
Now, output.csv has the contents of all csv files in all subdirectories.
So, now open a new cmd and type the following commands. Read the output carefully:
for /?
if /?
echo /?
type /?
Some suggestions for further reading:
https://ss64.com/nt/for_cmd.html
https://ss64.com/nt/for_r.html
https://ss64.com/nt/syntax-redirection.html
What does "&&" in this batch file?
Thanks to all of you for your help. After reading all the responses, I realized that I could get around the windows sorting problem by sorting into a second file and then reading it from there.
dir /b /s /O:N *.csv | sort > file.txt
for /f %%A IN (file.txt) do (
if not %%~nxA == output.csv (
echo %%~dpnxA
echo %%~nxA >> output.csv
type %%A >> output.csv
echo. >> output.csv
echo. >> output.csv
)
)
Again, thank you for all your help.

Batch: Filter filename

I have lot file that need to filter before move to other folder.
The filter condition:
[PEQ]*_[+-][1-9][0-9]_[P-R][0-9]_*[._][0-9]*
Example of filename:
P101_+19_R0_3.0_QA.txt
I try to apply the filter in batch script but nothing happen.
Am i correctly defined the filter?
My script:
SET _ext1=txt
SETLOCAL ENABLEDELAYEDEXPANSION
for %%f in (%_source%\[PEQ]*_[+-][1-9][0-9]_[P-R][0-9]_*[._][0-9]*.%_ext1%) do (
echo %%f >> %LOG%
SET _path=%%~df%%~pf
echo !_path! >> %LOG%
SET _filename=%%~nf
echo !_filename! >> %LOG%
echo. >> %LOG%
echo Processing !_filename! >> %LOG%
IF EXIST !_path!!_filename!*.%_ext1% (
copy /Y "!_path!!_filename!*.%_ext1%" "%_target%" >> %LOG%
)
)
ENDLOCAL
I hope you understand the difference between a wildcard * matching any number of any chars and the RegEx * which means any number (also zero) of the previous match. Together with the RE . any char .* will resemble the wildcard *
#Echo off & SETLOCAL ENABLEDELAYEDEXPANSION
SET _ext1=txt
for /f "delims=" %%f in (
'dir /B "%_source%\*_*_*_*.%_ext1%" ^| Findstr /i "%_source%\[PEQ]*_[+-][1-9][0-9]_[P-R][0-9]_.*[._][0-9]*.%_ext1%" '
) do (
echo %%f >> %LOG%
SET _path=%%~df%%~pf
echo !_path! >> %LOG%
SET _filename=%%~nf
echo !_filename! >> %LOG%
echo. >> %LOG%
echo Processing !_filename! >> %LOG%
IF EXIST !_path!!_filename!*.%_ext1% (
copy /Y "!_path!!_filename!*.%_ext1%" "%_target%" >> %LOG%
)
)
ENDLOCAL
You should first test the line with the dir and findstr in an open cmd window,
then without the escaping ^ and surrounding '
dir /B "%_source%\*_*_*_*.%_ext1%"
If this fits as a first selection add
dir /B "%_source%\*_*_*_*.%_ext1%" | Findstr /i "%_source%\[PEQ]*_[+-][1-9][0-9]_[P-R][0-9]_.*[._][0-9]*.%_ext1%"
And vary your RegEx until it does what you want.
Remember that findstr RegEx capabilities are quite limited.

Batch file to merge specific files into one?

I am newbie with windows batch command so please spare me for any irrelevant help.
Basically i want to merge certain files using type command of windows but since those files are coming from various sources i need to search in file name for source filter and only merge those files. i have tried writing below code but it's not doing the job for me.
#echo off
set filter=%1
set final_file=%2
echo %filter%
echo %final_file%
for %f in (*.dlt) do(
echo %f
if find %filter "%f (
do type "%f" >> %final_file
)
)
Here is an example that i made to merge all *.bat files in one file; so you can easily modify it to your needs :
Just you need to modify the variable Set "Filter_Ext=dlt" and the Set "MasterFolder=%userprofile%\desktop" to yours
#echo off
Mode 75,3 & Color 9E
Title Merge all *.bat in one file
Set "MasterFolder=%userprofile%\desktop"
Set "OutPut=Output_Merged_Files.txt"
Set "Filter_Ext=bat"
If exist "%OutPut%" Del "%OutPut%"
echo(
echo Please Wait a while we generate the output file ...
#For /f "delims=" %%a in ('Dir /s /b /A-D "%MasterFolder%\*.%Filter_Ext%"') Do (
cls
echo(
echo Please Wait a While ... Merging "%%~nxa" ...
(
echo ====================================================
echo Contents of "%%a"
echo ====================================================
Type "%%a"
echo(
)>> "%OutPut%"
)
Start "" "%OutPut%"
Edit Merge all .dlt in one file
#echo off
Mode 75,3 & Color 9E
Title Merge all *.dlt in one file
Set "MasterFolder=%~1"
Set "OutPut=Output_Merged_Files.txt"
Set "Filter_Ext=dlt"
Set "KeyWord=Engine"
If exist "%OutPut%" Del "%OutPut%"
echo(
echo Please Wait a while we generate the output file ...
#For /f "delims=" %%a in ('Dir /s /b /A-D "%MasterFolder%\*.%Filter_Ext%" ^|find /I "%KeyWord%"') Do (
cls
echo(
echo Please Wait a While ... Merging "%%~nxa" ...
(
echo ====================================================
echo Contents of "%%a"
echo ====================================================
Type "%%a"
echo(
)>> "%OutPut%"
)
Start "" "%OutPut%"

How to do while loop in windows batch

I wrote a windows batch script to check and move files to another directory based on the list I put in a notepad file named list.txt. But I have no idea that how to set the while-loop. Only to jump out of the subroute when the condition fulfill.
In C Programming, we could write like this by setting a while-loop direcly. But seems in windows batch is quite different.
All I want is like this:
Directory A:
1. A.txt
2. B.txt
3. list.txt (By line sequential with filename want to move)
4. process.bat
Directory B:
None of files (Then move a file by order of line set in list.txt) OR
A.txt (If already existed a text file in directory, process.bat will pause before A.txt disappear)
Process.bat
#echo off
:readline
for /f "tokens=*" %%a in (list.txt) do call :processline %%a
goto :eof
:processline
if exist D:\DirectoryA\*.txt (
echo %time% >> D:\AutoLog\Log.txt
echo Previous job did not finished yet. >> D:\AutoLog\Log.txt
Timeout /t 30
echo.
)
set name=%*
if exist %name%.txt (
echo %time% >> D:\AutoLog\Log.txt
echo File found and processing %name%.txt now... >> D:\AutoLog\Log.txt
copy "%~dp0\%name%.txt" "D:\DirectoryB"
echo Transfer %name%.txt completed!! >> D:\AutoLog\Log.txt
echo. >> D:\AutoLog\Log.txt
Timeout /t 790
echo.
echo ==============================================================
)
:eof
Please guide me to finish the script by using a while-loop method. Thanks
I change some script sequence and it works now.
#echo off
:readline
for /f "tokens=*" %%a in (list.txt) do call :processline %%a
goto :eof
:processline
set name=%*
if exist C:\Test2\*.txt (
echo %date% %time% >> C:\Test2\Log.txt
echo Previous job did not finished yet. >> C:\Test2\Log.txt
Timeout /t 5
echo.
echo. >> C:\Test2\Log.txt
goto :processline
)
if exist %name%.txt (
echo %date% %time% >> C:\Test2\Log.txt
echo File found and processing %name%.txt now... >> C:\Test2\Log.txt
copy "%~dp0\%name%.txt" "C:\Test2"
echo Transfer %name%.txt completed!! >> C:\Test2\Log.txt
echo. >> C:\Test2\Log.txt
Timeout /t 10
echo.
echo ==============================================================
)
:eof
This will copy as well as count the number of lines from your text file..
# echo off
:TextPath
cls
set /p Input=#1 Enter the full path of the text file :
set /p Source=#2 Enter the full path of Source :
set /p Target=#3 Enter the full path of Destination :
:choice
set /P c=Ready to Continue[Y/N]?
if /I "%c%" EQU "Y" goto :Yes
if /I "%c%" EQU "N" goto :No
goto :choice
:Yes_Local
for /f "delims=" %%i in (%Input%) do echo f| xcopy /f /y "%Source%\%%i" "%Target%\%%i"
for /f %%C in ('Find /V /C "" ^< %Input%') do set Count=%%C
msg * No of Lines executed= %Count%
exit
:No
cls
color e
echo Redirecting to Main....
PING 127.0.0.1 -n 2 >NUL
cls
echo Please wait
PING 127.0.0.1 -n 4 >NUL
goto TextPath

Recursive search by file list

I have a TXT file with a list of names like:
Test
Word
etc.
I need to search ( recursive, so also in subfolder ) if one of that name match with a file. This is my attempt:
#echo off
Set MyPath=C:\Folder
for /f %%i in (list.txt) do (
echo File with word %%i: >> result.txt
echo. >> result.txt
findstr /M /C:%%i /S "%MyPath%\*.*" >> result.txt
echo. >> result.txt
)
The result.txt i want is:
File with word test:
C:\Folder\test.exe
C:\Folder\my test.txt
C:\Folder\another test.doc
C:\Folder\tatest.bat
File with word Word:
C:\Folder\This is my word.exe
C:\Folder\CoolWord.txt
C:\Folder\hello word.bat
So how to improve that batch and make it recursive also for the subfolder of the initial dir?
Thanks
Test this: It will write result.txt with the results for each word.
#echo off
(
for /f "usebackq delims=" %%a in ("list.txt") do (
echo "File with word %%a:"
echo.
set "flag="
for /r "c:\folder" %%b in ("*%%a*") do (echo %%b&set flag=1)
if not defined flag echo No Matches
echo.
)
)>"result.txt"
dir /s /b C:\Folder\*.* >allfiles.txt
for /f %%i in (list.txt) do (
echo File with word %%i: >> result.txt
echo. >> result.txt
findstr /M /C:%%i /S "allfiles.txt" >> result.txt
echo. >> result.txt
)
#echo off
Set MyPath=C:\Folder
(for /f "delims=" %%i in (list.txt) do (
echo File with word %%i:
echo/
dir /B /S "%MyPath%\*%%i*"
echo/
)) > result.txt

Resources