Page 1 of 1
[SOLVED] Batch Script ignores folders with spaces
Posted: 28 Jan 2025 08:54
by PAB
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.
Re: Batch Script ignores folders with spaces
Posted: 28 Jan 2025 12:49
by Lucky4Me
Use (type "%IFN%") and type "%%f"
Re: Batch Script ignores folders with spaces
Posted: 28 Jan 2025 16:21
by PAB
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.
Re: [SOLVED] Batch Script ignores folders with spaces
Posted: 29 Jan 2025 13:49
by PAB
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.
Re: [SOLVED] Batch Script ignores folders with spaces
Posted: 29 Jan 2025 16:30
by Lucky4Me
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=
Re: [SOLVED] Batch Script ignores folders with spaces
Posted: 29 Jan 2025 17:18
by Lucky4Me
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$"
Re: [SOLVED] Batch Script ignores folders with spaces
Posted: 29 Jan 2025 19:10
by PAB
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.
Re: [SOLVED] Batch Script ignores folders with spaces
Posted: 02 Feb 2025 05:05
by Lucky4Me
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
Re: [SOLVED] Batch Script ignores folders with spaces
Posted: 24 Feb 2025 06:37
by PAB
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.