Ivaylo
Ivaylo

Reputation: 25

split an argument and add include the items in url

I'm trying to solve a problem at work, where it's required to access project folders on a shared drive.

However the naming convention is a bit tricky. The URLs on the server start with the static FS\XXX\00 followed by the project number (6 digits long) which is split into pieces of two and between slashes. For example the project folder for project 123456 would look like FS\XXX\00\12\34\56.

What I'm trying to sort out is how to create a .bat file, put it in the environment path and call it with the Run command, so for example I would call the file ex.bat by entering the following sequence in the Run console:

ex 123456

Then the program should split the number, build up and open the following URL:

FS\XXX\00\12\34\56

Any ideas?

Upvotes: 1

Views: 64

Answers (2)

Stephan
Stephan

Reputation: 56180

%1 is the first parameter. Save it to a variable (%p%) do be able to do substring substitution (see set /?) and build and output the desired string:

@echo off
set p=%1
echo FS\XXX\00\%p:~0,2%\%p:~2,2%\%p:~4,2%
pause

Upvotes: 2

Liam Binns-Conroy
Liam Binns-Conroy

Reputation: 36

Because powershell was tagged, here's a powershell implementation. Save this as a script in the environment path somewhere:

[CmdLetBinding()]
param
(
    [string] $ProjectNumber = '123457'
)

$root = 'FS\XXX\00\'

# Create a string with backslashes every 2 chars
$out = (&{for ($i = 0;$i -lt $ProjectNumber.length;$i += 2)
{
$ProjectNumber.substring($i,2)
}}) -join '\'

# Create final path
$path = Join-Path -Path $root -ChildPath "$out\"

# Run explorer with the path
explorer $path

and call from run/cmd like this:

powershell "& "Script.ps1 -ProjectNumber 123456""

Upvotes: 1

Related Questions