i have many folder in current path.need batch file to combine all *-140301-loan.txt to 1…
- User
i have many folder in current path.need batch file to combine all *-140301-loan.txt to 1 file. all text file utf8 encoding.
- Assistant
- User
i have a tiny tools combiner.exe that combine all text into one, change bat file above to use this tools instead to powersell. syntax: combiner.exe inputfilesyntax*.txt
- Assistant
To modify the batch file to use
combiner.exefor combining your text files instead of PowerShell, you can adjust the script as follows.Assuming that you want to use
combiner.exewith the input syntax for your files (*-140301-loan.txt), here's how to do it:- Open Notepad or any text editor.
- Copy the following modified script into it:
@echo off setlocal enabledelayedexpansion REM Define the output file name set outputFile=combined-loan.txt REM Remove the output file if it exists if exist "%outputFile%" del "%outputFile%" REM Build the input files list for combiner.exe set "inputFiles=" REM Loop through all subdirectories to find matching files for /r %%f in (*-140301-loan.txt) do ( echo Adding: %%f set "inputFiles=!inputFiles! %%f" ) REM Use combiner.exe to combine files if not "!inputFiles!"=="" ( combiner.exe !inputFiles! > "%outputFile%" echo All files have been combined into %outputFile% ) else ( echo No matching files found. ) endlocal - User
change structure to copy all (*-140301-loan.txt) from subfolder , to current folder,run combiner.exe *-140301-loan.txt and after finish command, del *-140301-loan.txt
- Assistant
You can adjust the batch file to copy all
*-140301-loan.txtfiles from subfolders to the current folder, runcombiner.exeon those files, and then delete them afterward. Here’s how you can do that:- Open Notepad or any text editor.
- Copy the following script into it:
- User
change below code to parameter 140201 in head of batch file:
@echo off setlocal enabledelayedexpansion
REM Define the output file name set outputFile=combined-loan.txt
REM Remove the output file if it exists if exist "%outputFile%" del "%outputFile%"
REM Copy matching files from subfolders to the current folder for /r %%f in (*-140201-loan.txt) do ( echo Copying: %%f copy "%%f" . )
REM Run combiner.exe on the copied files if exist (*-140201-loan.txt) ( echo Combining files... combiner.exe *-140201-loan.txt echo All files have been combined into %outputFile% ) else ( echo No matching files found to combine. )
REM Delete the copied files del *-140201-loan.txt ren MergeFile.txt KOL-140201.txt
echo Finished cleanup of temporary files. endlocal
- Assistant
To modify the batch file to take a parameter for the date suffix (in this case,
140201) at the beginning of the file, you can use to reference the first command-line argument. Here’s how you can adjust your script: - User
change below code to parameter 140201 in head of batch file::
@echo off setlocal enabledelayedexpansion
REM Copy matching files from subfolders to the current folder for /r %%f in (*-140201-loan.txt) do ( echo Copying: %%f copy "%%f" . )
REM Run combiner.exe on the copied files if exist (*-140201-loan.txt) ( echo Combining files... combiner.exe *-140201-loan.txt echo All files have been combined into %outputFile% ) else ( echo No matching files found to combine. )
REM Delete the copied files del *-140201-loan.txt ren MergeFile.txt KOL-140201.txt
echo Finished cleanup of temporary files. endlocal
- Assistant
To convert your batch script so that it accepts a date parameter (like
140201) at the beginning of the file, you can follow these adjustments:- The script will take the date as a command-line argument.
- You will check if the argument is provided and use it to modify the file matching patterns.
Here’s how the revised script would look:
@echo off setlocal enabledelayedexpansion REM Check if a parameter was provided if "%~1"=="" ( echo Please provide the date suffix as a parameter (e.g., 140201). exit /b 1 ) REM Define the date suffix from the first parameter set "dateSuffix=%~1" REM Copy matching files from subfolders to the current folder for /r %%f in (*-%dateSuffix%-loan.txt) do ( echo Copying: %%f copy "%%f" . ) REM Run combiner.exe on the copied files if exist (*-%dateSuffix%-loan.txt) ( echo Combining files... combiner.exe *-%dateSuffix%-loan.txt echo All files have been combined. ) else ( echo No matching files found to combine. ) REM Delete the copied files del *-%dateSuffix%-loan.txt ren MergeFile.txt KOL-%dateSuffix%.txt echo Finished cleanup of temporary files. endlocal - User
error : . was unexpected at this time.
- Assistant
The error message "
.was unexpected at this time" usually indicates that there is a syntax error in the batch file.In the script provided, the problem likely arises from the way the conditional checks and commands are structured, especially when dealing with filenames. The syntax for checking the existence of files in the statement may need to be modified to ensure it is being used correctly.