Solved Can you help me please ?

October 22, 2020 at 12:45:18
Specs: Windows 7
Hi All. I don't speak very well english. I use google translate to understand and type in english.

This script renames files and subdirectories, but not sub-subdirectories. What must be changed to rename only the sub-subdirectories but not the files?

Thank you.

The script:

@echo off
setLocal EnableDELAYedeXpansion
pushd *

for /f "tokens=* delims= " %%i in ('dir/b/s') do (

if /i not "%%~xi"==".bat" (

if not "%%~i"=="%~nx0" (

ren "%%i" !RANDOM!
)
)
)

message edited by tease_60


See More: Can you help me please ?


✔ Best Answer
October 26, 2020 at 21:42:15
Touche. Re-visited post #1, and you described it well. My bad! Also much more of a challenge than I expected, ha ha! Trying this:
@echo off>temp.bat & setlocal enabledelayedexpansion
set root=c:\test
for /f "tokens=*" %%a in ('dir %root% /b /s /ad ^|sort /r') do >>temp.bat echo ren "%%a" !random!
type temp.bat
echo Please test first on duplicates. last chance to bail out: ctrl-C
pause
rem call temp.bat

I left the call to temp.bat disabled. This seemed to work, but it still leaves open the possibility of duplicate random names - slim, but real. Whether that matters or not is up to you. If so, it can be refined to fix the problem. For now, I'll let you run your own tests and wait for the inevitable negative feedback! Make SURE to back up your test-set before running a final.


#1
October 22, 2020 at 20:32:07
Long, dark shot:
@echo off & setlocal enabledelayedexpansion
set root=c:\test
pushd %root%
for /f "tokens=*" %%a in ('dir /b /s /ad') do call :aa "%%a"
goto :eof

:aa
rem random does not guarantee a unique value, only a random value.
for /f "tokens=*" %%b in ('dir /b *.*') do call :cc "%~1\%%b"
goto :eof

:cc
rem Only leaving 'echo', not actual execution, for obvious testing eval.
if /i "%~x1" neq ".bat" echo ren %1 %random% 2>nul||goto :cc

message edited by nbrane


Reply ↓  Report •

#2
October 23, 2020 at 01:37:30
Hello and thank you
I created the test directory in C
I created sub-directories and sub-sub-directories
I copied the script, I deleted echo, I ran the batch but it doesn't work.
Where is the problem ?

@echo off & setlocal enabledelayedexpansion
set root=c:\test
pushd %root%
for /f "tokens=*" %%a in ('dir /b /s /ad') do call :aa "%%a"
goto :eof

:aa
for /f "tokens=*" %%b in ('dir /b *.*') do call :cc "%~1\%%b"
goto :eof

:cc
if /i "%~x1" neq ".bat" ren %1 %random% 2>nul||goto :cc


Reply ↓  Report •

#3
October 23, 2020 at 19:06:39
@echo off & setlocal enabledelayedexpansion
set root=c:\test
pushd %root%
:zz
for /f "tokens=*" %%a in ('dir /b /s /ad') do call :aa "%%a"
goto :eof

:aa
echo examining %1
rem random does not guarantee a unique value, only a random value.
for /f "tokens=*" %%b in ('dir /b /a-d "%~1"\*.*') do call :cc "%~1\%%b"
goto :eof

:cc
if /i "%~x1" equ ".bat" goto :eof
:dd
ren %1 %random% 2>nul||goto :dd

That's all I can do. Signing off on this one. Good luck.

message edited by nbrane


Reply ↓  Report •

Related Solutions

#4
October 24, 2020 at 13:44:35
Hi nbrane. It also doesn't work

Reply ↓  Report •

#5
October 24, 2020 at 19:09:59
I ran it on my end and it worked. It did NOT rename the subdirs, only the files within them, but it DID rename the files except for any batch files (.bat extension). Re-posted current script, which I used for testing, in post #3 (I hate wasting posts - RECYCLE!)

Reply ↓  Report •

#6
October 26, 2020 at 13:53:47
Hi nbrane.

I tested your script again. It only renames files in the directory, subdirectories, and sub-subdirectories.
I requested a script that renames the subdirectories and sub-subdirectories but not the files.
That's why I said it doesn't work.

(Before) Folder --> Subfolder --> Sub-Subfolder --> Sub-Sub-Folder --> ...

I want that

(After) Folder --> 4214 --> 8232 --> 658 --> ...

Thanks and see you later.


Reply ↓  Report •

#7
October 26, 2020 at 21:42:15
✔ Best Answer
Touche. Re-visited post #1, and you described it well. My bad! Also much more of a challenge than I expected, ha ha! Trying this:
@echo off>temp.bat & setlocal enabledelayedexpansion
set root=c:\test
for /f "tokens=*" %%a in ('dir %root% /b /s /ad ^|sort /r') do >>temp.bat echo ren "%%a" !random!
type temp.bat
echo Please test first on duplicates. last chance to bail out: ctrl-C
pause
rem call temp.bat

I left the call to temp.bat disabled. This seemed to work, but it still leaves open the possibility of duplicate random names - slim, but real. Whether that matters or not is up to you. If so, it can be refined to fix the problem. For now, I'll let you run your own tests and wait for the inevitable negative feedback! Make SURE to back up your test-set before running a final.

Reply ↓  Report •

#8
October 30, 2020 at 01:33:35
Hi nbrane

Thank you very much and good luck. Bye!


Reply ↓  Report •

Ask Question