张绍峰
张绍峰

Reputation: 311

How to operate file with dots in its name by Windows cmd?

For example

E:\2012.12.25  NO.23  Peter\Dailylog.txt
E:\2012.12.27  NO.12  John\log.txt
E:\2012.12.29  NO.25  Amy\log.txt
E:\2012.1.1  NO.23  Peter\bug .txt
E:\2013.1.2  NO.12  John\recoder.xml
E:\2012.1.3  NO.12  John\log.txt

Then, I want to put all John's folders into a folder; like this

E:\John\2012.12.27  NO.12  John\log.txt
E:\John\2013.1.2  NO.12  John\recoder.xml
E:\John\2012.1.3  NO.12  John\log.txt

I have tried

move "E:\*John" E:\John

but failed.

Any help would be appreciated.

Upvotes: 0

Views: 3926

Answers (1)

Mofi
Mofi

Reputation: 49086

The command MOVE used with directory names does not move the directory, but renames the directory.

The batch code below produces for the input example the output:

E:\Amy\2012.12.29  NO.25  Amy\log.txt
E:\John\2012.12.27  NO.12  John\log.txt
E:\John\2013.1.2  NO.12  John\recoder.xml
E:\John\2012.1.3  NO.12  John\log.txt
E:\Peter\2012.12.25  NO.23  Peter\Dailylog.txt
E:\Peter\2012.1.1  NO.23  Peter\bug .txt

Here is the batch code with comment lines between main code at top and the subroutine at bottom doing the folder movements.

@echo off
for /F "delims=" %%I in ('dir /AD /B E:\*.*.*NO.* 2^>nul') do call :CheckFolder "E:\%%I"
goto :EOF

rem The subroutine CheckFolder first splits up each folder name passed
rem to the subroutine into strings using dot and space as delimiters.

rem Of interest is just the sixth string which is assigned to loop
rem variable B which can contain itself dots and spaces. The first
rem 5 strings (year, month, day, NO and number) are not of interest
rem for determining the target folder name for the current folder.

rem A folder which can't be split up into at least 6 strings is ignored
rem for further processing as this is most likely one of the target folders.

rem The target folder is next created if not already existing. If creation
rem of target folder fails, the current folder with files to move is ignored.

rem Next all files in current folder are moved to the target folder and
rem the current folder is removed which is only successful if the current
rem folder contains now no files or subfolders.

:CheckFolder
set "FolderName="
for /F "tokens=5* delims=. " %%A in ("%~nx1") do set "FolderName=%%B"
if "%FolderName%" == "" goto :EOF
if exist "%~dp1%FolderName%\%~nx1" goto MoveFolder
md "%~dp1%FolderName%\%~nx1"
if errorlevel 1 goto :EOF

:MoveFolder
move /Y "%~1\*" "%~dp1%FolderName%\%~nx1\" >nul
rd "%~1"
goto :EOF

Edit according to comment regarding E:\2012.12.29 NO.25 Amy(later)\log.txt.

On processing a folder name like 2012.12.29 NO.25 Amy(later) the command line

for /F "tokens=5* delims=. " %%A in ("%~nx1") do set "FolderName=%%B"

assigns to ignored loop variable A the string 25 and to loop variable B the string Amy(later) which is assigned next to environment variable FolderName.

To get just Amy from this folder name it would be necessary to change this code line to

for /F tokens^=6^ delims^=^(.^  %%A in ("%~nx1") do set "FolderName=%%A"

Now an opening parenthesis is also interpreted as string delimiter like a dot and a space and just string 6 which is Amy is assigned to loop variable A which is assigned next to environment variable FolderName.

The strange looking syntax for the options is needed here as ( has a special meaning for Windows command interpreter and therefore can't be easily specified as string delimiter as literal character.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • dir /?
  • echo /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • move /?
  • rd /?
  • rem /?
  • set /?

See also the Microsoft article Using command redirection operators for an explanation of >nul (suppress standard output) and 2>nul (suppress error output) whereby the redirection operator > is escaped on 2>nul with caret character ^ to be interpreted as redirection operator on execution of command DIR and not already on parsing the FOR command line.

Upvotes: 3

Related Questions