简体   繁体   中英

How to split string with windows batch

I have a code that creates a bulk folder for each file and drags the file into the corresponding folder, but I am trying to shorten (split) the string.

For example, convert

L2021-56378 Sample Documentation Form 
L2022-56378 Sample Documentation Form
L2023-56378 Sample Documentation Form

to just

L2021-56378
L2022-56378
L2023-56378

Below is my code:

@echo off
for %%i in (*) do (
 if not "%%~ni" == "organize" (
  md "%%~ni" && move "%%~i" "%%~ni"
 )
)
...
 if not "%%~ni" == "organize" (
  for /f %%e in ("%%~ni") do (
   ECHO %%e
   ECHO md "%%e" 2>nul&& ECHO move "%%~i" "%%e"
  )
 )
...

should do that - I'm assuming you want a directory named L2021-56378 and to move the file to that directory.

The ECHO keywords are present to report the intended operations to the screen for verification. Remove them if the commands are correct to actually do the md and move .

See for /? from the prompt or many, many items on SO for documentation. The for/f uses default options tokens=1 and delims includes Space

----- Applying this to your code,

@echo off
for %%i in (*) do (


 if not "%%~ni" == "organize" (
  for /f %%e in ("%%~ni") do (
   ECHO %%e
   ECHO md "%%e" 2>nul&& ECHO move "%%~i" "%%e"
  )
 )


)

works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM