Renaming Hidden and System files Command

18,708

When using ren you must remove hidden and system flags, rename the file, then set the flags again:

attrib -s -h desktop.ini
ren desktop.ini desktop.txt
attrib +s +h desktop.txt

If you can't do that, you have to use something else, e.g. VBScript:

Set fso = CreateObject("Scripting.FileSystemObject")
fso.GetFile(WScript.Arguments(0)).Name = WScript.Arguments(1)

or PowerShell:

Rename-Item 'desktop.ini' 'desktop.txt' -Force
Share:
18,708
Built on Sin
Author by

Built on Sin

Updated on July 25, 2022

Comments

  • Built on Sin
    Built on Sin almost 2 years

    I'm trying to figure out a way to rename Desktop.ini to *.ini and back again. The problem is I need to do this with the attributes in tact (So I cannot remove hidden and system and then rename). The only solution I have come up with is xcopy and del but I am having trouble with the syntax on options, source options and destination options.

    This is what I have come up with:

    @ECHO OFF
    
    xcopy /H /R Desktop.ini Desktop.txt /K
    del /Q /AHS Desktop.ini
    xcopy /H /R Desktop.txt Desktop.ini /K
    del /Q /AHS Desktop.txt
    
    Pause
    
    exit /b
    
  • Built on Sin
    Built on Sin almost 11 years
    That would be fine if I merely wanted to delete the files. I need to rename Desktop.ini (With attributes hidden and system) to . and back again (With same attributes)
  • Built on Sin
    Built on Sin almost 11 years
    As I stated originally I need the attributes in tact, I cannot get rid of the attributes and read them without having to rename the file again. "The problem is I need to do this with the attributes in tact (So I cannot remove hidden and system and then rename)."
  • Marc.2377
    Marc.2377 over 5 years
    Note to googlers: -s marks a system file, while -h means a normal hidden file. en.wikipedia.org/wiki/File_attribute#DOS_and_Windows