Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
PAB
- Posts: 146
- Joined: 12 Aug 2019 13:57
#1
Post
by PAB » 28 Jan 2025 08:54
Good afternoon,
I have this script which works great on folders that do
NOT have any spaces in the filename . . .
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "IFN=I:\Test\Paul\Paul No Way\*.*"
set "OFN=COPIED_to_SINGLE_FILE.log"
if exist %OFN% (del /f /q %OFN% >nul 2>&1)
echo %IFN% &:: This shows the path with a space in it correcctly !
set /a Counter=-1
for %%f in (type %IFN%) do (set /a Counter+=1
type %%f >> %OFN%
echo.>> %OFN%
echo.#####################################################################################################################################################################>> %OFN%
echo.>> %OFN%
) 1>nul 2>&1
echo Total = %Counter% >> %OFN%
Start /MAX "" "%OFN%"
pause
Obviously the code above is just a stripped down version, but it still replicates the problem that I am having.
I have tried using single and double quotes etc, among other things but without success.
Thank you very much in advance for any help given.
Last edited by
PAB on 28 Jan 2025 16:23, edited 1 time in total.
-
PAB
- Posts: 146
- Joined: 12 Aug 2019 13:57
#3
Post
by PAB » 28 Jan 2025 16:21
Good evening,
Can you hear that noise Lucky4Me ?
It is me slapping my weary old head.
It worked perfectly, thank you SO much.
Best Regards, Paul.
-
PAB
- Posts: 146
- Joined: 12 Aug 2019 13:57
#4
Post
by PAB » 29 Jan 2025 13:49
Just one more question please if I may.
How can I get all files EXCEPT
.jpg,
.mp4 and
.png.
Instead of . . .
Code: Select all
set "IFN=I:\Test\Paul\Paul No Way\*.*"
Or would it be something in here maybe ? . . .
Thanks again in advance.
-
Lucky4Me
- Posts: 28
- Joined: 21 Oct 2020 06:33
#5
Post
by Lucky4Me » 29 Jan 2025 16:30
You can try this
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "IFN=I:\Test\Paul\Paul No Way\"
set "files=*.txt *.log *.json"
set "OFN=COPIED_to_SINGLE_FILE.log"
if exist %OFN% (del /f /q %OFN% >nul 2>&1)
echo %IFN% &:: This shows the path with a space in it correcctly !
set /a Counter=0
for /r "%IFN" %%f in (%files%) do (set /a Counter+=1
type "%%f" >> %OFN%
echo.>> %OFN%
echo.#####################################################################################################################################################################>> %OFN%
echo.>> %OFN%
) 1>nul 2>&1
echo Total = %Counter% >> %OFN%
Start /MAX "" "%OFN%"
pause
This will combine all .txt .log and .json in the directory. You can add more extentions to set files=
-
Lucky4Me
- Posts: 28
- Joined: 21 Oct 2020 06:33
#6
Post
by Lucky4Me » 29 Jan 2025 17:18
You can also use the following code
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "IFN=I:\Test\Paul\Paul No Way\*.*"
set "excludefiles=\.mp4$ \.jpg$ \.png$"
set "OFN=COPIED_to_SINGLE_FILE.log"
if exist %OFN% (del /f /q %OFN% >nul 2>&1)
echo %IFN% &:: This shows the path with a space in it correcctly !
set /a Counter=0
for /f "delims=" %%f in ('dir /b /s "%IFN%" ^| findstr /v /i "%excludefiles%"') do (
set /a Counter+=1
type "%%f" >> %OFN%
echo.>> %OFN%
echo.#####################################################################################################################################################################>> %OFN%
echo.>> %OFN%
) 1>nul 2>&1
echo Total = %Counter% >> %OFN%
Start /MAX "" "%OFN%"
pause
This is with an exclude file list, you can add extentions by "\.ext$"
-
PAB
- Posts: 146
- Joined: 12 Aug 2019 13:57
#7
Post
by PAB » 29 Jan 2025 19:10
Thank you so much Lucky4Me for taking the time to help me. It is appreciated.
BOTH codes work great.
I will be using the 'Exclude' code as there are very few 'File Types' that are relevant to my needs. The 'Exclude' code also lists the files output in the selected directory in alphabetical order which unfortunately the first code doesn't. I will have another look at the first code when I get back from the hospital.
Thanks once again.
-
Lucky4Me
- Posts: 28
- Joined: 21 Oct 2020 06:33
#8
Post
by Lucky4Me » 02 Feb 2025 05:05
This is another version of the "include Files" and the files will be in alphabetical order.
I also adapted some of the info that goes into the log file, if you don't want it you can remove it from the batch.
I shows you the filename that's being processed and at the end the total of processed files.
Before it logs an file it will create 3 lines, 2 lines with ##### and the middle one will give you the counternumber and path\filename as info.
Code: Select all
@echo off & setlocal EnableDelayedExpansion
mode 120,6
set "line="
for /L %%i in (1,1,120) do set "line=!line!#"
set "IFN=I:\Test\Paul\Paul No Way\*.*"
set "includefiles=\.txt$ \.log$"
set "OFN=COPIED_to_SINGLE_FILE.log"
if exist %OFN% (del /f /q %OFN% >nul 2>&1)
set /a Counter=0
for /f "delims=" %%f in ('dir /b /s "%IFN%" ^| findstr /i "%includefiles%"') do (
set /a Counter+=1
cls
echo.&echo.
echo Logging... %%f
echo.&echo.
echo !line!>> %OFN%
echo !Counter! %%f>> %OFN%
echo !line!>> %OFN%
type "%%f">> %OFN%
echo.>> %OFN%
)
echo.&echo !line!>> %OFN%
echo Total = !Counter!>> %OFN%
cls
echo.&echo.&echo Total = %Counter% files logged to %OFN%
echo.
Start /MAX "" "%OFN%"
pause
Regards Lucky4Me
-
PAB
- Posts: 146
- Joined: 12 Aug 2019 13:57
#9
Post
by PAB » 24 Feb 2025 06:37
Thanks for another version Lucky4Me.
Sorry I haven't replied earlier but I have been in hospital !
This being old is not all its cracked up to be !
Anyway, thank you VERY much again, it is appreciated.