I’ve got this little batch file I’m trying to write for a Windows 7 environment. It’s supposed to go through a directory and echo the last four characters of the file names. So far I’ve got:
@ECHO OFF
SETLOCAL
for /r C:\Users\userName\Desktop\testFolder %%g in (*) do (
Set fileName = %%~ng
echo %fileName:~-4%
)
And all that is echoed out is “~-4” once for each file in testFolder. I can’t figure out what’s wrong, but then I’m not very well versed in batch files or dos. Any help would be appreciated, thanks.
Environment variable expansion occurs when the command is read, so your
%fileName:~-4%is evaulated when theforis read, which is before theSetis performed. Use delayed expansion.Note also that spaces are significant in the
Setcommand. With the space, you created a variable calledfileNamewith a trailing space.