D
D
Djasar2021-04-27 09:07:06
cmd/bat
Djasar, 2021-04-27 09:07:06

Need to sort all files by folders.?

I'm not very strong in CMD,
there are files in the folder C: \ TEMP \
A20210423.2300+0300-0000+0300_ATS_60_01_GU.XML.gz
a20210423.2300+0300-0000+0300_60_02_guln.xml.gz
a20210423.23.23.23.23.23.23.23.23.23.23.23.23.23.23.23.23.23.23.23.23AL .xml.gz
A20210423.2345+0300-0000+0300_ATS_60_02_GULN.xml.gz
you need to create folders by file name from the end of the file name and decompose into folders.

Should happen here
folder with: \ the Temp \ ATS_60_01_GU
A20210423.2300 + 0300-0000 + 0300_ATS_60_01_GU.xml.gz
A20210423.2345 + 0300-0000 + 0300_ATS_60_01_GU.xml.gz

folder C: \ the Temp \ ATS_60_02_GULN
A20210423.2300 + 0300- 0000+0300_ATS_60_02_GULN.xml.gz
A20210423.2345+0300-0000+0300_ATS_60_02_GULN.xml.gz

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2021-04-27
@Djasar

Powershell

Set-Location c:/temp
$groups = (Get-ChildItem A*).Name | group-object -property {$_.Substring(30,$_.Length -37)} -ashashtable -asstring
$groups.Keys | foreach-object { 
    $folder=$_; $groups[$folder] | foreach-object {
                  new-item -itemtype file -path "$folder/$_" -force
                  move-item -path $_ -destination "$folder/$_" -force
     }
}

If you need a one-liner
cd c:/temp;$g=(dir A*).Name | group {$_.Substring(30,$_.Length -37)} -AHT -asstring;$g.keys | %{$f=$_;$g[$f] | %{ni -type file -path $f/$_ -force; mv $_ $f/$_ -force}}

The one-liner can be passed in single quotes as a parameter to powershell.exe if you need to use this functionality in already written cmd scripts.
But in general, you don’t need to dig out the stewardess to use cmd for scripts. Use powershell\python

R
res2001, 2021-04-27
@res2001

There are several problems in your task:
1. Remove the double expansion. If the extension is always fixed, then you can simply use the replace operation that is in set. If the extension is not fixed, then 2 procedure calls (or nested for loops) and the %%~dpnI.
2. Select a substring in the file name. The file name itself can be distinguished from a path from which extensions have already been previously removed using the modifier %%~nI. Then you can use a loop for /fto split the name into component parts, specify the characters "+", "-", "_" as a separator.
Files are bypassed in the for loop.
For reference see:

:: тут описание модификаторов и описание разновидностей for
for /?
:: операция замены, арифметика, ...
set /?
:: Вам нужно будет, скорее всего использовать режим отложенного расширения переменных: ENABLEDELAYEDEXPANSION. Разберитесь с ним, это достаточно тонкий и не очевидный момент
setlocal /?
:: просто так
if /?
:: Каждая команда cmd имеет свою справку используйте ее. Это основной источник информации по программированию cmd
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question