Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 455 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert long filename to short filename (8.3) using cmd.exe

#1
I am trying to convert a long filename to a short filename (8.3) on Windows.

A batch-file with a command line argument works as intended:

*short.bat*:

@echo OFF
echo %~s1

calling `short.bat C:\Documents and Settings\User\NTUSER.DAT` returns ` C:\DOCUM~1\USER\NTUSER.DAT`


However, I don't like having an extra .bat-file for this. I would rather call `cmd.exe` with the whole command from a ruby script.
How can I do this?

As an intermediate step I tried to hardcode the path in the batch-file, but that does not work:

*short1.bat*:

@echo OFF
SET filename="C:\Documents and Settings\User\NTUSER.DAT"
echo %filename%
echo %~sfilename%

`echo %filename%` works, but `echo %~sfilename%` gives the following error:

The following usage of the path operator in batch-parameter
substitution is invalid: %~sfilename%

For valid formats type CALL /? or FOR /?

If *short1.bat* works, how can I convert this into a one-liner that can be called with `cmd.exe \c ...`?

There is another question (

[To see links please register here]

), however that one is specifically asking for the path of the current directory.
Reply

#2
Replace the filename.txt to the filename you want to convert to 8.3

dir /x filename.txt

You will then have to split the result with whitespace as your delimiter (\s in regex).
Then the value with the ~ is your short filename. If your filename is short to begin with, then you won't find a string containing a ~.
Reply

#3
cmd /c for %A in ("C:\Documents and Settings\User\NTUSER.DAT") do @echo %~sA
Reply

#4
Here is an example that read in the registry the location of your "appdata\local" folder and convert it to short path:


cls
@echo off
cd /d "%~dp0"
chcp 65001 >nul

for /f "skip=1" %%a in ('"wmic useraccount where name='%USERNAME%' get sid"') do (
for %%b in (%%a) do set current_SID=%%b
)
set current_username=%USERNAME%
set current_userprofile=%USERPROFILE%

set key_to_read=HKEY_USERS\%current_SID%\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
set value_to_read=Local AppData
rem If value_to_read contains ? space(s) set tokens to 2+?
for /f "usebackq eol= tokens=3,* delims= " %%a in (`reg query "%key_to_read%" /v "%value_to_read%" 2^>nul ^| find "%value_to_read%"`) do (
set value_type=%%a
set data_read=%%b
)
set data_read=%data_read:USERPROFILE=current_userprofile%
call set "data_read=%data_read%"
set current_local_appdata=%data_read%

set current_local_appdata_temp=%current_local_appdata%\Temp
echo %current_local_appdata_temp%

for %%a in ("%current_local_appdata_temp%") do set "current_local_appdata_temp_short=%%~sa"
echo %current_local_appdata_temp_short%

pause
exit
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through