How to remove spaces from file names (in bulk)

前端 未结 6 2083
无人及你
无人及你 2020-12-04 19:51

How to remove spaces (not replace with underscores) from several thousand files in bulk in Windows? Can I do this from the DOS command?

Currently:

fi         


        
相关标签:
6条回答
  • 2020-12-04 20:13

    Create a powershell file - *.ps1 extension

    Write this code:

    dir |
    Where-Object { $_.name.Contains(" ") } |
    Rename-Item -NewName { $_.name -replace " ","" }
    

    save, then right click -> run with powershell

    0 讨论(0)
  • 2020-12-04 20:23

    In Windows:

    1. Open a Command Prompt.
    2. Go to the folder with the cd command (eg.: cd "paht of your folder").
    3. Open a powershell by typing: powershell
    4. Then input this: get-childitem *.mp3 | foreach {rename-item $_ $_.name.replace(" ","")}
    0 讨论(0)
  • 2020-12-04 20:24

    Here is a script that can efficiently bulk rename files, stripping all spaces from the name.

    :renameNoSpace  [/R]  [FolderPath]
    @echo off
    setlocal disableDelayedExpansion
    if /i "%~1"=="/R" (
      set "forOption=%~1 %2"
      set "inPath="
    ) else (
      set "forOption="
      if "%~1" neq "" (set "inPath=%~1\") else set "inPath="
    )
    for %forOption% %%F in ("%inPath%* *") do (
      if /i "%~f0" neq "%%~fF" (
        set "folder=%%~dpF"
        set "file=%%~nxF"
        setlocal enableDelayedExpansion
        echo ren "!folder!!file!" "!file: =!"
        ren "!folder!!file!" "!file: =!"
        endlocal
      )
    )
    

    Assume the script is called renameNoSpace.bat

    renameNoSpace : (no arguments) Renames files in the current directory

    renameNoSpace /R : Renames files in the folder tree rooted at the current directory

    renameNoSpace myFolder : Renames files in the "myFolder" directory found in the current directory.

    renameNoSpace "c:\my folder\" : Renames files in the specified path. Quotes are used because path contains a space.

    renameNoSpace /R c:\ : Renames all files on the C: drive.

    0 讨论(0)
  • 2020-12-04 20:31

    The problem i have faced is that there is a possibility that there is already a file with the name you try to give to the new file (eg if there are 2 files in the folder named "file one.txt" and "file_one.txt" when you try to replace the spaces with underscores, one file will replace the other). So I made this script that checks if the new name already exists and if so places a number at the end of the file name (adds 1 to the number until there is no other file with that name). Instructions about what to change are at the top (commended out lines). Do not store the batch file in the same folder you have the files to be renamed if you use *.* option. I hope this helps.

    @echo off
    
    REM Instructions
    REM This script repaces spaces from file names with underscores. 
    REM If you want to just remove the spaces uncomment lines 30 and 52 and comment out the lines 29 and 51. 
    REM set the following parameters. 
    REM pb is the folder containing the files we want to rename (fullpath)
    REM tm is a temporary folder that will be created and deleted. Just put a folder that does not exist and is not used by anything else (fullpath).
    REM all is the file type you want to raname. E.g. *.* for every file, *.txt for TXTs, *.pdf for PDFs etc 
    REM you don't have to change anything else
    
    set pb=<folder containing the files to rename>
    set tm=<a temp folder that does not exist>
    set all=*.*
    
    set pa=%pb%%all%
    
    setlocal EnableDelayedExpansion
    
    cd /d %pa%
    
    set /a count=1
    
    if not exist %tm% mkdir %tm%
    
    for /f %%F in (%pa%) do (
    
        set name=%%~nF
        set name2=!name: =_!
        REM set name2=!name: =!
        set name3=!name2!%%~xF
    
        if !name2! == %%~nF ( 
            move /y %%~dpF\!name3! %tm%\ >nul
        ) else (
                if not exist %%~dpF\!name3! ( 
                    if not exist %tm%\!name3! (
                        ren "%%F" "!name3!" 
                        move /y %%~dpF\!name3! %tm%\ >nul
                    )
            )   
        ) 
    
    )
    
    :rename
    
    for /f %%F in (%pa%) do (
    
        set name=%%~nF
        set name2=!name: =_!
        REM set name2=!name: =!
        set name4=!name2!%count%
        set name3=!name4!%%~xF
    
        if !name2! == %%~nF ( 
            move /y %%~dpF\!name3! %tm%\ >nul
        ) else (
                if not exist %%~dpF\!name3! ( 
                    if not exist %tm%\!name3! (
                        ren "%%F" "!name3!" 
                        move /y %%~dpF\!name3! %tm%\ >nul
                    )
            )   
        ) 
    
    )
    
    set /a count = %count% + 1
    
    set /a loop = 0
    
    for %%F in (%pa%) do (set /a loop = 1)
    
    if %loop% equ 1 goto rename
    
    move /y %tm%\%all% %pb% >nul
    
    rmdir /s /q %tm%
    
    0 讨论(0)
  • 2020-12-04 20:33

    You can write a simple script that does this for one file/directory, e.g.:

    @echo off
    setlocal enableextensions enabledelayedexpansion
    
    set "ARG=%~1"
    ren "%ARG%" "%ARG: =%"
    

    ...and then if you'd like, run it over all the files and/or directories you care about. For instance, if you create the above script as myrenamingscript.cmd, you can run it over all non-dir files in the current dir by running:

    for %f in (*) do @myrenamingscript.cmd "%~f"
    
    0 讨论(0)
  • 2020-12-04 20:38
    @echo off
    setlocal enableextensions enabledelayedexpansion
    
    for %%f in (*.*) do (
    set ARG=%%~nxf
    rename "%%f" !ARG: =!
    )
    
    0 讨论(0)
提交回复
热议问题