-1

Im trying to change the name of folders inside a folder to the name of the folders owner.. Exemple, I have a folder with one hundred other folder's all with the name "My Documents" and all of the with a diferente owner. I already find a way to find the name with the code: dir /q but I have no ideia on how to put it in a variable and use that variable to change the folders name...

  • You can use something like `for /F "tokens=4" %%O in ('dir /A:D /Q') do (echo %%O)` to get the owner, supposing it does not contain white-spaces -- type `for /?` into command prompt and read the help text carefully... note that the output of `dir` depends on the locale and region settings of the system... – aschipfl May 18 '16 at 14:58

2 Answers2

0

The usual way to get a commands output is a for.

@echo off
for /f "skip=2 tokens=4,*" %%a in ('dir /q /ad^|find "<DIR>"') do (
  for /f "tokens=2 delims=\" %%i in ("%%a") do (
    echo owner=%%i
    echo folder=%%b
    echo ren "%%b" "%%i"
  )
)
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • That appears to work (no errors on cmd) but the files simply dont change... :\ – Norberto Gil May 18 '16 at 15:16
  • it is usus on this site to "unarm" potentially dangerous code. Remove the `echo` in front of `ren` when you verified that the output is ok. – Stephan May 18 '16 at 15:20
  • hmm now it get "Sintax is incorrect" because its using the name as "domain\user" ... can I remove everything before the"\" – Norberto Gil May 18 '16 at 15:38
0

Here is a batch script -- let us call it rename-dir-by-owner.bat -- based on the code in my comment:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

for /F "delims=" %%U in ('
    dir /Q /A:D "%~1" ^| find "<DIR>" ^| findstr /V /R /C:" \.*\.$"
') do (
    set "ITEM=%%U"
    setlocal EnableDelayedExpansion
    set "ITEM=!ITEM:*<DIR>=!" & set "ITEM=!ITEM:*\=!"
    for /F "tokens=1,* delims= " %%O in ("!ITEM!") do (
        endlocal
        ECHO ren "%~dp1%%P" "%%O"
        setlocal
    )
    endlocal
)

endlocal
exit /B

To use this script, provide the directory containing the ones subject to rename as a command line argument -- like this, for instance, supposing the directory is called parent-folder:

rename-dir-by-owner.bat "parent-folder"

The upper-case ECHO command is there for testing purposes; remove it as soon as you actually want to rename any items.


Explanation

The output of the core dir /Q /A:D command line used in the above script looks similar to this:

 Volume in drive D has no label.
 Volume Serial Number is XXXX-XXXX

 Directory of D:\Data

2016/01/01  00:00    <DIR>          NT AUTHORITY\SYSTEM    .
2016/01/01  00:00    <DIR>          BUILTIN\Administrators ..
2015/07/01  01:00    <JUNCTION>     NT AUTHORITY\SYSTEM    AppData [C:\Users\aschipfl\AppData\Roaming]
2016/03/21  20:00    <DIR>          HOST\aschipfl          Documents
2016/05/18  23:00    <DIR>          HOST\aschipfl          Drawings
2016/04/01  13:00    <DIR>          HOST\aschipfl          Projects
               0 File(s)              0 bytes
               6 Dir(s)   2'147'483'648 bytes free

The find command filters out all lines that do not contain <DIR> (which marks directories), findstr removes the dummy directories . (current) and .. (parent). So the following lines remain:

2016/03/21  20:00    <DIR>          HOST\aschipfl          Documents
2016/05/18  23:00    <DIR>          HOST\aschipfl          Drawings
2016/04/01  13:00    <DIR>          HOST\aschipfl          Projects

These lines are captured by the outer for /F loop, each one is stored in variable ITEM. Afterwards everything up to the portion <DIR> is removed, then everything up to the (first) \ is cut off. The remaining content of ITEM is now parsed with another for /F loop, which extracts everything in front of the first white-spaces, so the user name of the owner is retrieved; behind the white-spaces is the directory name. Both string parts are passed over to the ren command finally (once ECHO is away).

With this approach the result does (almost) not depend on the locale and region settings of the system, so a quite general solution is achieved. However, this batch program leads to unexpected results if a user name contains white-spaces, but such is not recommended by Microsoft anyway -- consult this article: Creating User and Group Accounts, section "Rules for Logon Names".

Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99