2

So, I have a batch that runs through a file system, and checks the size of the files in it and renames them with a different extension if the size is zero bytes. That part was easy, but now I have to check to see if what's currently being looked at is a file or a folder.

here's my code so far.

SET FTPFOLDER="C:\temp"

rem this goes thru a folder system.
for /f %%f in ('dir /b %FTPFOLDER%') do (call :Search "%%f") 
EXIT /B 0


REM GOES THRU EACH OF THE FILES IN EACH OF THE FOLDERS
:Search
for /f %%g in ('dir /b %FTPFOLDER%\%~1') do(call :checkType "%%g", "%FTPFOLDER%\%~1\%%g\", %%~ng  )
pause
EXIT /B 0

:checkType
if exists %~1\* (call :Search %~1) else (call :checkSize %~2, %~3)
EXIT /B 0

REM CHECKS IF ZERO 
:checkSize
set file=%~1
@echo %file%
for /f "usebackq" %%h in ('%file%') do set size=%%~zh
if %size% EQU 0 (call :Rename "%file%", %~2)
EXIT /B 0

REM - RENAMES FILE TO .EMPTY
:Rename
ren %~1 %~2.empty 
EXIT /B 0

Nothing I've tried so far has worked. I am extremely new to batch, so any and all help would be appreciated, especially with explanations.

Edit: I feel that it's also important to mention that no file names or folder names will have spaces.

Edit: I have changed my code to remove the extra function, and do it in the for loop. My new code is as follows

SET FTPFOLDER="C:\Users\nmavis\Desktop\gndjufsb"

rem this goes thru a folder system. 
for /f %%f in ('dir /b %FTPFOLDER%') do (call :Search "%%f") 
EXIT /B 0

REM GOES THRU EACH OF THE FILES IN EACH OF THE FOLDERS
:Search
for /f %%g in ('dir /b %FTPFOLDER%\%~1') do (if exist %%~sg\NUL (echo dir) else (echo file))
pause
EXIT /B 0

REM CHECKS IF ZERO 
:checkSize
set file=%~1
@echo %file%
for /f "usebackq" %%h in ('%file%') do set size=%%~zh
if %size% EQU 0 (call :Rename "%file%", %~2)
EXIT /B 0

REM - RENAMES FILE TO .EMPTY
:Rename
ren %~1 %~2.empty 
EXIT /B 0

However, whenever I run it, no matter whether or not it is a file or a folder, it displays "File"

Intrinsic Nerd
  • 97
  • 1
  • 11
  • Type `dir /?`; you'll find a switch `/A` which allows to filter for directories `/A:D` or files `/A:-D`... – aschipfl Jul 14 '16 at 17:27
  • Can you clarify what you mean? Like where in the code should I do this, and what exactly does it mean? – Intrinsic Nerd Jul 14 '16 at 17:31
  • Sorry, I think `dir /A` is not applicable for you as you try to distinguish between files and folders in `:checkType` and not earlier, so I am going to delete my related comment... BUT if it helps you, let me know: what I meant: `dir /A:D` returns only directories, whereas `dir /A:-D` returns only files; `dir` with neither of these filter options returns both... – aschipfl Jul 14 '16 at 17:33
  • I was about to say `for %%I in ("%file%") do if exist %%~sI\NUL echo directory`. Looks like that's already the accepted answer in the question aschipfl linked. – rojo Jul 14 '16 at 17:34
  • @rojo, a plain `for` should return files only anyway in case it accesses the file system (so when a wildcard is used), unless the `/D` is provided, in which case directories are enumerated only... – aschipfl Jul 14 '16 at 17:37
  • I already looked at the link you posted way before I posted this and couldn't get it to work @aschipfl. But if you think that any different formatting might work, I can try, because I don't recall seeing `%%~s` on the page. I also don't know what %~s does though, so it's possible I just skimmed over it without realizing – Intrinsic Nerd Jul 14 '16 at 17:49
  • I just looked, and I see what you're talking about, I'll check to see if that works for me. I didn't realize file names wouldn't necessarily work – Intrinsic Nerd Jul 14 '16 at 17:51
  • @aschipfl You're thinking of wildcard matching. Without wildcards, `for` treats the parenthetical parameters as a string, not as a filesystem object. – rojo Jul 14 '16 at 18:14
  • @aschipfl I just made an edit that may be easier to help me fix. Thanks so far for all of the help guys! – Intrinsic Nerd Jul 14 '16 at 18:25
  • @rojo you too! I'm starting to understand a bit more – Intrinsic Nerd Jul 14 '16 at 18:25

1 Answers1

0

After a bit of searching, I fixed the problem by changing this line:

for /f %%g in ('dir /b %FTPFOLDER%\%~1') do (if exist %%~sg\NUL (echo dir) else (echo file))

to this:

for /f %%g in ('dir /b %FTPFOLDER%\%~1') do (if exist %FTPFOLDER%\%~1\%%g\* (call :Search "%~1\%%g")  else (call :checkSize "%FTPFOLDER%\%~1\%%g", "%%~ng"))

which not only determines whether or not it's a folder or file, but also properly will go a level deeper in the file system, no matter how many files deep you are.

Intrinsic Nerd
  • 97
  • 1
  • 11