1. ホーム
  2. スクリプト・コラム
  3. DOS/BAT

DOS(バット)文字列置換の原理

2022-02-10 11:26:14

文字列 "c:/test/1" の中の "/" をすべて "//" に置き換える原理です。

(文字列 "C:/TEST/1" がすでに VAR 変数に存在すると仮定して:)

C:/WINDOWS>set "var=C:/TEST/1"

(変数varの文字列中の"/"を全て"//"に置き換える)

C:/WINDOWS>set "var=%var:/=//%"

(変数varの中身を表示する)

C:/WINDOWS>echo %var%.
C:///TEST//1

という結果になりました。C://TEST//1、これは完全に置き換えられました。

以下は、他のユーザーから寄せられた例です。

ファイル文字列置換

typedef struct_SID_AND_ATTRIBUTES{
PSID Sid;
DWORD Attributes;
}SID_AND_ATTRIBUTES;
 
typedef struct_LUID_AND_ATTRIBUTES{
LUID Luid;
DWORDAttributes;
} LUID_AND_ATTRIBUTES;

例2

// RestrictToken.cpp
//
 
#include "stdafx.h"
#include "windows.h"
 
int_tmain(int argc, _TCHAR* argv[])
{
 
//
// Create a SID for the BUILTIN\Administrators group
//
BYTE sidBuffer[256] = {0};
 
// PSID variable-length data structure
PSID pAdminSID = (PSID)sidBuffer;
 
/*typedef struct_SID_IDENTIFIER_AUTHORITY {
  BYTE Value[6];
} SID_IDENTIFIER_AUTHORITY,*PSID_IDENTIFIER_AUTHORITY;
*/
SID_IDENTIFIER_AUTHORITY SIDAuth =SECURITY_NT_AUTHORITY;
 
/*
BOOL WINAPIAllocateAndInitializeSid(
  __in PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
  __in BYTE nSubAuthorityCount,
  __in DWORD dwSubAuthority0,
  __in DWORD dwSubAuthority1,
  __in DWORD dwSubAuthority2,
  __in DWORD dwSubAuthority3,
  __in DWORD dwSubAuthority4,
  __in DWORD dwSubAuthority5,
  __in DWORD dwSubAuthority6,
  __in DWORD dwSubAuthority7,
  __out PSID *pSid
);
*/
if( !AllocateAndInitializeSid( &SIDAuth,
                                     2,
                                     SECURITY_BUILTIN_DOMAIN_RID,
                                     DOMAIN_ALIAS_RID_ADMINS,
                                     0,
                                     0,
                                     0,
                                     0,
                                     0,
                                     0,
                                     &pAdminSID))
{
   printf( "AllocateAndInitializeSid Error%u\n", GetLastError() );
   return -1;  
}
 
 
//
// Change the local administrator's SID to a deny-only SID
//
 
 
/*
typedef struct_SID_AND_ATTRIBUTES{
     PSID Sid;
     DWORD Attributes;
}SID_AND_ATTRIBUTES;
*/
SID_AND_ATTRIBUTES SidToDisable[1] = {0};
SidToDisable[0].Sid = pAdminSID;
SidToDisable[0].Attributes = 0;
 
//Get the Token of the current process
HANDLE hOldToken = NULL;
if(!OpenProcessToken(
   GetCurrentProcess(),                  
   TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE |
   TOKEN_QUERY | TOKEN_ADJUST_DEFAULT,
   &hOldToken))
{
   printf("OpenProcessToken failed(%lu)\n", GetLastError() );
   return -1;
}
 
 
/*
BOOL WINAPICreateRestrictedToken(
  __in HANDLE ExistingTokenHandle,
  __in DWORD Flags,
  __in DWORD DisableSidCount, // Disable SID with
  __in_opt PSID_AND_ATTRIBUTES SidsToDisable,
  __in DWORD DeletePrivilegeCount, //delete privileges with
  __in_opt PLUID_AND_ATTRIBUTES PrivilegesToDelete,
  __in DWORD RestrictedSidCount,
  __in_opt PSID_AND_ATTRIBUTES SidsToRestrict,
  __out PHANDLE NewTokenHandle
);
*/
// Create a restrictedtoken based on the Token of the current process
HANDLE hNewToken = NULL;
if(!CreateRestrictedToken(hOldToken,
   DISABLE_MAX_PRIVILEGE,
   1, SidToDisable,
   0, NULL,
   0, NULL,
   &hNewToken))
{
   printf("CreateRestrictedToken failed(%lu)\n", GetLastError());
   return -1;
}
 
if(pAdminSID)
   FreeSid(pAdminSID);
 
 
// The following codecreates a new process
// with the restrictedtoken.
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory(&si, sizeof(STARTUPINFO) );
si.cb = sizeof(STARTUPINFO);
si.lpDesktop = NULL;
 
 
// Receive the target process path in order to create the restricted process
charszSysDir[MAX_PATH+1] = {0};
printf("pleaseinput app path for restricted token: ");
scanf("%s",szSysDir);
 
/*
BOOL WINAPI CreateProcessAsUser(
  __in_opt HANDLE hToken,
  __in_opt LPCTSTR lpApplicationName,
  __inout_opt LPTSTR lpCommandLine,
  __in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
  __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
  __in BOOL bInheritHandles,
  __in DWORD dwCreationFlags,
  __in_opt LPVOID lpEnvironment,
  __in_opt LPCTSTR lpCurrentDirectory,
  __in LPSTARTUPINFO lpStartupInfo,
  __out LPPROCESS_INFORMATION lpProcessInformation
);
*/
 
if(!CreateProcessAsUser(
   hNewToken,
   szSysDir, NULL,
   NULL, NULL,

著者はそれを少し変更する
16を17に変更

@echo off 
setlocal enabledelayedexpansion 
set file=c:\Program Files\hndsclient\ds\GetFile.ini
set "file=%file:"=%" 
for %%i in ("%file%") do set file=%%~fi 
echo. 
set replaced=16 
echo. set replaced=16 
set all=17
for /f "delims=" %%i in ('type "%file%"') do ( 
  set str=%%i 
  set "str=!str:%replaced%=%all%! " 
  echo !str!>>"%file%"_tmp.txt 
) 
copy "%file%" "%file%"_bak.txt >nul 2>nul 
move "%file%"_tmp.txt "%file%" 
start "" "%file%"