Home > OS >  How to create directory from URL path separator (forward slash) when that is present in FileName cod
How to create directory from URL path separator (forward slash) when that is present in FileName cod

Time:07-26

As I mentioned in Q-title, I have setup a sublime settings auto-syncing process to Github Gist of my choice when I have passed correct Access Token.

Now this process syncs any file that is present inside a folder(s), considering the relative paths, as DirName/FileName.extension where DirName and/or FileName may contain singular spaces( ) between words(if >2 words in either).

Now when I download that gist thro' DownloadZIP button, it ofcourse downloads the whole gist with multiple files as single zip, which after download can be extracted into it's folder in local system(Windows 10/11 OS).

So, the question finally: When I have downloaded such zip and extracted into folder that contains one/multiple files that have / or / in their names, how do I create a directory from those filenames and move those files into those directory thro' a batch file if possible, or a PowerShell command, but runnable from Batch script, and then also rename those files to remove all DirName/ parts from their names ? I know about mv and md commands but how do I create directory from URL coded character group and do those operations ?

Note: DirName/ parts could be occurring in multiples like DirName1/DirName2/... if the file that is being synced from local is inside mainfolder/subfolders... relatively.

CodePudding user response:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET "#name1=DirName%/"
SET "#name2=DirName1%/DirName2%/"
SET "#name3=DirName1%/DirName2%/"
SET "#2f=%/"
SET "#subs=ABC"

SET #

FOR /f "delims=" %%e IN ("%#2f%") DO (
 FOR /f "delims=" %%w IN ("!#name1:%#2f%=%#subs%!") DO ECHO REN "%#name1%" "%%w"
 FOR /f "delims=" %%w IN ("!#name2:%#2f%=%#subs%!") DO ECHO REN "%#name2%" "%%w"
 FOR /f "delims=" %%w IN ("!#name3:%#2f%=%#subs%!") DO ECHO REN "%#name3%" "%%w"
)

ECHO ===================

FOR %%b IN ("%#name1%" "%#name2%" "%#name3%") DO (
 SET "#name=%%~b"
 FOR /f "delims=" %%e IN ("%#2f%") DO (
  FOR /f "delims=" %%w IN ("!#name:%#2f%=%#subs%!") DO ECHO REN %%b "%%w"
 )
)

GOTO :EOF

This demo shows how in batch.

CodePudding user response:

You can try :

[System.Web.HttpUtility]::UrlDecode("DirName/FileName.extension")

it gives :

DirName/FileName.extension

Then you can use the CmdLets with noun "Path" to manage your file names.

Get-Command -Noun path

If it exists some / in place of \, Powershel should manage them.

The to work with files and folders is given with CmdLet with noun Item :

Get-Command -Noun item

For example to create a directory :

New-Item -name toto -ItemType Directory
  • Related